What is the best way to change the following program into functions?
Can someone show me as a good programming style?
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
// Variables
char R, playing, again;
int roll, roll1, roll2, roll3, round, money, wager, win, loss, initial;
string name;
int main()
{
cout <<"Let's Play Craps!" << endl;
cout <<"This program simulates a session at a craps table!\n"<< endl;
cout <<"What's your first name, slick? ";
cin >> name;
cout <<"How much money do you have? ";
cin >> money;
initial = money;
cout << endl;
round = 1;
win = 0;
loss = 0;
do // main while loop which has other nested loops
{
cout << "Round " << round << endl;
round ++;
cout << endl;
cout << "How much do you wager? ";
cin >> wager;
while (wager > money)
{
cout << "Error, enter a smaller amount.";
cout << endl;
cout << "How much do you wager?";
cin >> wager;
}
srand(unsigned(time(NULL))); // intializes the random number generator
roll1 = rand() % 6 + 1; // generates a number between 1 & 6
roll2 = rand() % 6 + 1; // generates a number between 1 & 6
roll = roll1+roll2; // combines roll1 & roll2 to give you first roll
cout << "Go shooter, roll the dice!\n" << endl;
cout << "Enter R to roll: ";
cin >> R;
cout << "Roll is " << roll << endl;
//FIRST DEPENDENCY
if (roll == 7 || roll == 11) // user wins right away on first roll
{
cout << "You win!" << endl;
win++;
money += wager;
char again;
cout << "Your balance is $" << money << endl;
cout << "Play again (Y/N)?";
cin >> again;
if (again == 'N')
break;
} // closes main if loop which roll is 7 or 12
else if (roll == 2 || roll == 3 || roll == 12 ) // will result in craps user loses
{
cout << "Craps!" ;
cout << "You lose!" << endl;
loss ++;
money -= wager;
char again;
cout << "Your balance is $" << money << endl;
cout << "Play again (Y/N)?";
cin >> again;
if (again == 'N')
break;
} // closes main if lop
//SECOND DEPENDENCY
int point = roll;
cout << "\nNow " << point << " is the point!" << " Press R to roll: ";
cin >> R;
do // second roll if user hasn't won or lost yet
{
srand(unsigned(time(NULL)));
roll1 = rand() % 6 + 1;
roll2 = rand() % 6 + 1;
roll3 = roll1 + roll2;
cout << "Roll is " << roll3 ;
if (roll3 == 7 || roll3 == roll)
break;
cout << " Press R to roll: ";
cin >> R;
} while (roll3 != point && roll3 != 7); // will close the do loop if roll3 is same as point or 7
if (roll3 == roll) // roll3 is equal to roll its an automatic win
{
cout << "\n\nYou win!" << endl;
win ++;
money += wager;
cout << "Your balance is $" << money << endl;
}
if (roll3 == 7) // if second roll is 7 player loses
{
cout << "\n\nYou loss!" << endl;
loss++;
money -= wager;
cout << "Your balance is $" << money << endl;
}
cout << "Play again (y/n)?";
cin >> again;
}while (again =='Y'); // closes main else
cout << "Number of games played: " << (round-1) << endl;
cout << "Number of wins: " << win << endl;
cout << "Number of losses: " << loss << endl;
cout << "\n" << name << " started with $" << initial << endl;
cout << name << " ended with $" << money << endl;
cout << "Average amount won per game: $" << ((money - initial) / (round-1))<< endl;
cout << name << " has left the building.\n" << endl;
system("pause");
return 0;
} // end main