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

Recommended Answers

All 4 Replies

What is the best way to change the following program into functions?

Figure out what statements you need in the function and what values you need to pass in.
Replace those statements with a function call.
Copy the statements to the top or bottom of your program surrounded by the function header and footer.
Define the now local values that are used in the new function.
Test....

Can someone show me as a good programming style?

See this

I see a few good candidates for functions here. A function generally tries to do one and only one thing. That's a general rule with plenty of exceptions. One thing that can be placed into a function is the welcome message. That's short enough that it might be worth combining that with where you ask the person his name and how much money he has. You can make either one function or two from that. Let's say you make it two separate functions. Heck, make it three functions, one for the welcome message, one to get the name, one to get the inital bet. Stick lines 15 and 16 into a function called Welcome(), lines 17 and 18 into function called GetName(), and lines 19 and 20 into a function called GetInitialMoney().

void Welcome()
{
    cout <<"Let's Play Craps!" << endl;
    cout <<"This program simulates a session at a craps table!\n"<< endl;
}


string GetName()
{
    string name;
    cout <<"What's your first name, slick? ";
    cin >> name;
    return name;
}


int GetInitialMoney()
{
    int money;
    cout <<"How much money do you have? ";
    cin >> money;
    return money;
}


int main()
{
    Welcome();
    name = GetName();
    money = GetIntialMoney();

    // more code here
}

Lines 32 to 41 can be placed into a function called GetWager(int money).

The function call would be this...

wager = GetWager(money);

Lines 44 to 46 could be a function called GetRoll().

It gets a little harder later. You might want to make a function and see whether a roll result is a win, loss, or neither. This will be less of a copy and paste than the other functions. The code will have to be redesigned to remove the print statement and the money adjustment. The function itself determines the game result and based on that, make your printouts and money adjustments elsewhere, either in main or in another function.

This is so far I have changed. How can I call the parameters.
For example, how to shares 'money' variable between different functions

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

// Variables
char  R, playing, again;
int roll, roll1, roll2, roll3, round, wager, money, win, loss, initial;
string name;

void Welcome();
void GetName();
void GetMoney();
void GameFunctions();
void FinalResults();



int main() 
{
	Welcome();
	GetName();
	GetMoney();
	GameFunctions();
	FinalResults();




	
	system("pause");
	return 0;


} // end main

void Welcome()
{
	cout <<"Let's Play Craps!" << endl;
	cout <<"This program simulates a session at a craps table!\n"<< endl;
}

void GetName()
{
	string name;
	cout <<"What's your first name, slick? ";
    cin >> name;
}

void GetMoney()
{
	int money;
	cout <<"How much money do you have? ";
    cin >> money;
	int initial = money;
    cout << endl;
}

void GameFunctions()
{
	     
     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
}
void FinalResults()
{
	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;
}

Look up how the RETURN statement works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.