random number guess

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 40
Reputation: student4lyfe is an unknown quantity at this point 
Solved Threads: 0
student4lyfe student4lyfe is offline Offline
Light Poster

Re: random number guess

 
0
  #11
Jul 25th, 2008
I got the enter void GetBet(int money, int& bet) function to work. it turns out that i called the int bet twice and then instead of using the while i used the if statement and it ran. Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 40
Reputation: student4lyfe is an unknown quantity at this point 
Solved Threads: 0
student4lyfe student4lyfe is offline Offline
Light Poster

Re: random number guess

 
0
  #12
Jul 25th, 2008
now I'm getting a bunch of error messages. now the program is saying that all of my varables are undefined and that my GetGuess function definition is illegal.
What is wrong with the guess function?
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <ctime>
  7. using namespace std;
  8. /* ----------------------------------------------------------------- */
  9. /* Function Prototypes */
  10. /* ----------------------------------------------------------------- */
  11. void PrintHeading(int money);
  12. void GetBet(int money, int& bet);
  13. int GetGuess(void);
  14. /* ----------------------------------------------------------------- */
  15. int main ()
  16.  
  17. {
  18.  
  19. srand((unsigned)time(NULL));
  20. int money = 1000;
  21.  
  22. int bet;
  23.  
  24. int ncount=0;
  25.  
  26. int numofgamesplayed=0;
  27. int avgnumOfGuesses=0;
  28. bool done = true;
  29. const int numMax = 7;
  30. const int numMin = 0;
  31. const int guessMax = 100;
  32. const int guessMin = 1;
  33. int choice =2 ;
  34. //char n;
  35. //char y;
  36. PrintHeading(money);
  37. GetBet( money, bet);
  38. GetGuess();
  39. do
  40. {
  41. num = (rand() + time(0)) % 100;
  42. done == false;
  43. ++numofgamesplayed;
  44. numOfGuesses = 1;
  45.  
  46.  
  47. while( choice != 2);
  48. cout<< endl << " Like to try the game again <y or n>? ";
  49. cin>> choice;
  50.  
  51. return 0;
  52. }
  53. /*
  54. PrintHeading simply prints the introductory output.
  55. Parameters: initial amount of money received
  56. */
  57.  
  58. void PrintHeading(int money)
  59.  
  60. {
  61.  
  62.  
  63. cout << "=============================================" << endl;
  64. cout << " Welcome to the High Low betting Game. " << endl;
  65. cout << " You have $" << money << " to begin game. " << endl;
  66. cout << " Valid guesses are number between 1 and 100. " << endl;
  67. cout << "=============================================" << endl << endl;
  68.  
  69. }
  70. /*
  71. GetBet prompts for and reads in a bet. The function performs all
  72. error checking necessary to insure that a valid bet is read in
  73. and does not return until a valid bet is entered.
  74. Parameters:
  75. money: the amount of money the player currently has
  76. bet: the bet chosen by the user
  77. */
  78. void GetBet(int money, int& bet)
  79. {
  80. //int bet;
  81. if(( bet < money ) || ( bet > 0))
  82. {
  83. cout << " Enter bet : ";
  84. cin >> bet;
  85. }}
  86. /*
  87. GetGuess reads in a guess. The user is not prompted for the guess in
  88. this function. The user only gets one chance to input a guess value.
  89. Return Value: the value of the guess if the input is valid
  90. 0 if the input guess was not valid
  91. */
  92. int GetGuess(void);
  93. {
  94. int guess;
  95. int num;
  96. int sum = 0;
  97. int numOfGuesses;
  98.  
  99. if (guess == num)
  100. {
  101. cout << " You guessed the correct number. " << endl << endl;
  102. done = true;
  103. }
  104. else
  105. if (guess < num)
  106. cout << " Your guess is lower than the number. " << endl << endl;
  107. else
  108. cout << " Your guess is higher than the number. " << endl << endl;
  109. }
  110. sum+= numOfGuesses ;
  111. cout << " Sorry... the correct answer was " << num << endl;
  112. cout<<"\n Number of Games played "<<numofgamesplayed;
  113. cout<<"\n Total number of guesses :"<< sum;
  114. cout<<"\n Guessing Average = :"<<sum/numofgamesplayed;
  115. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,827
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: random number guess

 
0
  #13
Jul 25th, 2008
  1. do
  2. {
  3. num = (rand() + time(0)) % 100;
  4. done == false;
  5. ++numofgamesplayed;
  6. numOfGuesses = 1;
  7.  
  8.  
  9. while( choice != 2);
  10. cout<< endl << " Like to try the game again <y or n>? ";
  11. cin>> choice;
  12.  
  13. return 0;
  14. }
  15. /*
  16. PrintHeading simply prints the introductory output.
  17. Parameters: initial amount of money received
  18. */
  19.  
  20. void PrintHeading(int money)

You have a brackets problem and a do loop that starts but never ends and thus return 0; and void PrintHeading(int money) are inside that do-loop that has a while that supposedly ends it, but in fact does not due to the lack of a bracket. If you format your code so that the brackets line up with each other and the do lines up with the while from an indentation standpoint, you will spot this much sooner. You need to take a step back and look at your whole program. You are coding in too many of the details of the program and the functions without looking at the overall program structure and what should go where and what function does what. Every opening bracket has a closing bracket. Every "do" needs a "while" at the end. Carefully look at each of the function specifications and read the words that describe what the function is supposed to do, what it doesn't do, what it returns, and the parameters it is passed. Also, every variable that you compare to another variable must be initialized to some value that makes sense before that comparison is made. I'm speaking in particular of this line 6 below. num and guess have completely random values. What does num represent? What does guess represent? How are they supposed to get their values? Take a step back and make sure you have a firm understanding of what exactly you want the program and the functions to do. You can't successfully code it until you know exactly what the goal is.

  1. int guess;
  2. int num;
  3. int sum = 0;
  4. int numOfGuesses;
  5.  
  6. if (guess == num)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 40
Reputation: student4lyfe is an unknown quantity at this point 
Solved Threads: 0
student4lyfe student4lyfe is offline Offline
Light Poster

Re: random number guess

 
0
  #14
Jul 26th, 2008
ok I had to take a break from the functins before I went crazy. I got the program to run with a bet but when it trys to walk passed the the bet I get an error. can someone help with my beting statement.
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <ctime>
  7. using namespace std;
  8. /* ----------------------------------------------------------------- */
  9. /* Function Prototypes */
  10. /* ----------------------------------------------------------------- */
  11. void PrintHeading(int money);
  12. void GetBet(int money, int& bet);
  13. /* ----------------------------------------------------------------- */
  14. int main ()
  15.  
  16. {
  17.  
  18. srand((unsigned)time(NULL));// generates a random number
  19. int money = 1000;// beginning value for the money the user has
  20. int num;// the number that the computer generates
  21. int bet;// the amount of money that the player wagers
  22. int guess;// the player guess
  23. int numOfGuesses;// number of times the player guesses
  24. int ncount=0;
  25. int sum=0;
  26. int numofgamesplayed=0;
  27. int avgnumOfGuesses=0;
  28. int addedBalance = 0;
  29. bool done;
  30. const int numMax = 7;
  31. const int numMin = 0;
  32. const int guessMax = 100;
  33. const int guessMin = 1;
  34. int choice =2 ;
  35. //char n;
  36. //char y;
  37. PrintHeading(money);
  38. GetBet( money, bet);
  39. do
  40. {
  41. num = (rand() + time(0)) % 100;
  42. done = false;
  43. ++numofgamesplayed;
  44. numOfGuesses = 1;
  45. if((bet > 0)||( bet <= money))
  46. {
  47. if (guess == num)
  48. money = bet / guess;
  49. else
  50. addedBalance = money - bet;
  51. }
  52. while ((numOfGuesses < 7) && (!done))
  53. {
  54. cout << " Guess " << numOfGuesses << " : ";
  55. cin >> guess;
  56. cout << endl;
  57. numOfGuesses++;
  58.  
  59. if (guess == num)
  60. {
  61. cout << " You guessed the correct number. " << endl << endl;
  62. done = true;
  63. }
  64. else
  65. if (guess < num)
  66. cout << " Your guess is lower than the number. " << endl << endl;
  67. else
  68. cout << " Your guess is higher than the number. " << endl << endl;
  69. }
  70. sum+= numOfGuesses ;
  71. avgnumOfGuesses = sum/numofgamesplayed;
  72. cout << " Sorry... the correct answer was " << num << endl;
  73. cout << "\n Number of Games played "<<numofgamesplayed << endl;
  74. cout << " You have $ " << addedBalance << " dollars left " << endl;
  75. cout << "\n Total number of guesses :"<< sum;
  76. cout << "\n Guessing Average = :"<<avgnumOfGuesses;
  77.  
  78. }
  79. while( choice != 2);
  80. cout<< endl << " Like to try the game again <y or n>? ";
  81. cin>> choice;
  82.  
  83. return 0;
  84. }
  85. /*
  86. PrintHeading simply prints the introductory output.
  87. Parameters: initial amount of money received
  88. */
  89.  
  90. void PrintHeading(int money)
  91.  
  92. {
  93.  
  94.  
  95. cout << "=============================================" << endl;
  96. cout << " Welcome to the High Low betting Game. " << endl;
  97. cout << " You have $" << money << " to begin game. " << endl;
  98. cout << " Valid guesses are number between 1 and 100. " << endl;
  99. cout << "=============================================" << endl << endl;
  100.  
  101. }
  102. /*
  103. GetBet prompts for and reads in a bet. The function performs all
  104. error checking necessary to insure that a valid bet is read in
  105. and does not return until a valid bet is entered.
  106. Parameters:
  107. money: the amount of money the player currently has
  108. bet: the bet chosen by the user
  109. */
  110. void GetBet(int money, int& bet)
  111. {
  112. //int bet;
  113. if(( bet < money ) || ( bet > 0))
  114. {
  115. cout << " Enter bet : ";
  116. cin >> bet;
  117. }}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,827
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: random number guess

 
0
  #15
Jul 26th, 2008
I don't know whether this is the error you are referring to or not, but it's an error that needs to be fixed:

  1. /*
  2. GetBet prompts for and reads in a bet. The function performs all
  3. error checking necessary to insure that a valid bet is read in
  4. and does not return until a valid bet is entered.
  5. Parameters:
  6. money: the amount of money the player currently has
  7. bet: the bet chosen by the user
  8. */
  9. void GetBet(int money, int& bet)
  10. {
  11. //int bet;
  12. if(( bet < money ) || ( bet > 0))
  13. {
  14. cout << " Enter bet : ";
  15. cin >> bet;
  16. }}

One, notice that the specification requires that the function does not return until a valid bet is entered. That means that there has to be some sort of a loop here. Currently you do not have one. You are on the right track with your condition, but not quite there.

  1. ( bet < money ) || ( bet > 0)

A valid bet requires that bet is less than money (you may want to change < to <=) AND that bet > 0 (if bets of 0 are allowed, change > to >=). The key word here is AND. Currently you || in your condition, which means "OR", not "AND".

You are also doing a comparison before there is any value assigned to bet. You don't want to do that. You need to have the user enter a bet before comparing bet to anything. So you need a loop of some type in this function, where the loop is exited when he/she enters a valid bet, and you need to make sure not to check for a valid bet until the user actually enters a bet. I would also display an error message if the user enters an invalid bet so that the he/she knows what he/she did wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 40
Reputation: student4lyfe is an unknown quantity at this point 
Solved Threads: 0
student4lyfe student4lyfe is offline Offline
Light Poster

Re: random number guess

 
0
  #16
Jul 26th, 2008
thax, now back to the hard stuff
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 40
Reputation: student4lyfe is an unknown quantity at this point 
Solved Threads: 0
student4lyfe student4lyfe is offline Offline
Light Poster

Re: random number guess

 
0
  #17
Jul 26th, 2008
i'm assuming with the additionof some code I some how killed my play again statement
could someone look at the statement Lines 83-85 for me?
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <ctime>
  7. using namespace std;
  8. /* ----------------------------------------------------------------- */
  9. /* Function Prototypes */
  10. /* ----------------------------------------------------------------- */
  11. void PrintHeading(int money); //prints header
  12. void GetBet(int money, int& bet); // gets bet
  13. /* ----------------------------------------------------------------- */
  14. int main ()
  15.  
  16. {
  17.  
  18. srand((unsigned)time(NULL)); // generates a random number
  19. int money = 1000; // beginning value for the money
  20. int num; // the number that the computer generates
  21. int bet; // the amount of money that the player bets
  22. int guess; // the player guess
  23. int numOfGuesses; // number of times the player guesses
  24. int sum=0; // total number of guesses
  25. int numofgamesplayed=0; // total number of games played
  26. int avgnumOfGuesses=0; // average number of games played
  27. int addedBalance = 0; // balance after money has been +-
  28. bool done;
  29. const int numMax = 7; // maximum number of guesses
  30. const int numMin = 0; // minimum number of guesses
  31. const int guessMax = 100; // max range of random number
  32. const int guessMin = 1;// min range of random number
  33. int choice;
  34.  
  35. PrintHeading(money);
  36. GetBet( money, bet);
  37.  
  38.  
  39. do
  40. {
  41. num = (rand() + time(0)) % 100;
  42. done = false;
  43. ++numofgamesplayed;
  44. numOfGuesses = 1;
  45.  
  46. while ((numOfGuesses < 7) && (!done))
  47. {
  48. cout << " Guess " << numOfGuesses << " : ";
  49. cin >> guess;
  50. cout << endl;
  51. numOfGuesses++;
  52.  
  53. if (guess == num)
  54. {
  55. cout << " You guessed the correct number. " << endl << endl;
  56. done = true;
  57. }
  58. else
  59. if (guess < num)
  60. cout << " Your guess is lower than the number. " << endl << endl;
  61. else
  62. cout << " Your guess is higher than the number. " << endl << endl;
  63. }
  64.  
  65. sum+= numOfGuesses ;
  66. if((bet > 0)&&( bet <= money))
  67. {
  68. if (guess == num)
  69. money = bet / guess;
  70. else
  71. addedBalance = money - bet;
  72. if ((bet > money)|| (bet < 0))
  73. cout << " Please enter a vaild bet " <<endl;
  74. }
  75. avgnumOfGuesses = sum/numofgamesplayed;
  76. cout << " Sorry... the correct answer was " << num << endl;
  77. cout << "\n Number of Games played "<<numofgamesplayed << endl;
  78. cout << " You have $ " << addedBalance << " dollars left " << endl;
  79. cout << "\n Total number of guesses :"<< sum;
  80. cout << "\n Guessing Average = :"<<avgnumOfGuesses;
  81.  
  82. }
  83. while( choice != 2);
  84. cout<< endl << " Like to try the game again <1 or 2>? ";
  85. cin>> choice;
  86.  
  87.  
  88. return 0;
  89. }
  90. /*
  91. PrintHeading simply prints the introductory output.
  92. Parameters: initial amount of money received
  93. */
  94.  
  95. void PrintHeading(int money)
  96.  
  97. {
  98.  
  99.  
  100. cout << "=============================================" << endl;
  101. cout << " Welcome to the High Low betting Game. " << endl;
  102. cout << " You have $" << money << " to begin game. " << endl;
  103. cout << " Valid guesses are number between 1 and 100. " << endl;
  104. cout << "=============================================" << endl << endl;
  105.  
  106. }
  107. /*
  108. GetBet prompts for and reads in a bet. The function performs all
  109. error checking necessary to insure that a valid bet is read in
  110. and does not return until a valid bet is entered.
  111. Parameters:
  112. money: the amount of money the player currently has
  113. bet: the bet chosen by the user
  114. */
  115. void GetBet(int money, int& bet)
  116. {
  117. //int bet;
  118. if(( bet < money ) || ( bet > 0))
  119. {
  120. cout << " Enter bet : ";
  121. cin >> bet;
  122. }}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,827
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: random number guess

 
1
  #18
Jul 26th, 2008
You need to format so things line up, particularly when you are first learning, but really even as you get better. Things will pop out to you when you line things up and spaces things properly. It becomes very obvious what is inside a block of code and what is outside of that block. If you are not already using an IDE like Visual Studio Express (there are others out there too), it will simplify your life greatly to start using one. It has a formatting feature. One click of a button and your code is formatted the way you want it. Here's your code formatted and lined up. It will look far better in an IDE where the comments don't overflow onto the next line, so copy and paste it into one and it'll look better. Look at lines 89 and 90. They are not inside of any loop. Do you want them there?


  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <ctime>
  7. using namespace std;
  8.  
  9.  
  10. /* ----------------------------------------------------------------- */
  11. /* Function Prototypes */
  12. /* ----------------------------------------------------------------- */
  13. void PrintHeading(int money); //prints header
  14. void GetBet(int money, int& bet); // gets bet
  15. /* ----------------------------------------------------------------- */
  16.  
  17.  
  18. int main ()
  19. {
  20. srand((unsigned)time(NULL)); // generates a random number
  21. int money = 1000; // beginning value for the money
  22. int num; // the number that the computer generates
  23. int bet; // the amount of money that the player bets
  24. int guess; // the player guess
  25. int numOfGuesses; // number of times the player guesses
  26. int sum=0; // total number of guesses
  27. int numofgamesplayed=0; // total number of games played
  28. int avgnumOfGuesses=0; // average number of games played
  29. int addedBalance = 0; // balance after money has been +-
  30. bool done;
  31. const int numMax = 7; // maximum number of guesses
  32. const int numMin = 0; // minimum number of guesses
  33. const int guessMax = 100; // max range of random number
  34. const int guessMin = 1;// min range of random number
  35. int choice;
  36.  
  37. PrintHeading(money);
  38. GetBet( money, bet);
  39.  
  40.  
  41. do
  42. {
  43. num = (rand() + time(0)) % 100;
  44. done = false;
  45. ++numofgamesplayed;
  46. numOfGuesses = 1;
  47.  
  48. while ((numOfGuesses < 7) && (!done))
  49. {
  50. cout << " Guess " << numOfGuesses << " : ";
  51. cin >> guess;
  52. cout << endl;
  53. numOfGuesses++;
  54.  
  55. if (guess == num)
  56. {
  57. cout << " You guessed the correct number. " << endl << endl;
  58. done = true;
  59. }
  60. else if (guess < num)
  61. cout << " Your guess is lower than the number. " << endl << endl;
  62. else
  63. cout << " Your guess is higher than the number. " << endl << endl;
  64. }
  65.  
  66. sum+= numOfGuesses ;
  67.  
  68. if((bet > 0)&&( bet <= money))
  69. {
  70. if (guess == num)
  71. money = bet / guess;
  72. else
  73. addedBalance = money - bet;
  74.  
  75. if ((bet > money)|| (bet < 0))
  76. cout << " Please enter a vaild bet " <<endl;
  77. }
  78.  
  79. avgnumOfGuesses = sum/numofgamesplayed;
  80. cout << " Sorry... the correct answer was " << num << endl;
  81. cout << "\n Number of Games played "<<numofgamesplayed << endl;
  82. cout << " You have $ " << addedBalance << " dollars left " << endl;
  83. cout << "\n Total number of guesses :"<< sum;
  84. cout << "\n Guessing Average = :"<<avgnumOfGuesses;
  85. }
  86. while( choice != 2);
  87.  
  88.  
  89. cout<< endl << " Like to try the game again <1 or 2>? ";
  90. cin>> choice;
  91.  
  92. return 0;
  93. }
  94.  
  95.  
  96. /*
  97. PrintHeading simply prints the introductory output.
  98. Parameters: initial amount of money received
  99. */
  100. void PrintHeading(int money)
  101. {
  102. cout << "=============================================" << endl;
  103. cout << " Welcome to the High Low betting Game. " << endl;
  104. cout << " You have $" << money << " to begin game. " << endl;
  105. cout << " Valid guesses are number between 1 and 100. " << endl;
  106. cout << "=============================================" << endl << endl;
  107. }
  108.  
  109.  
  110. /*
  111. GetBet prompts for and reads in a bet. The function performs all
  112. error checking necessary to insure that a valid bet is read in
  113. and does not return until a valid bet is entered.
  114. Parameters:
  115. money: the amount of money the player currently has
  116. bet: the bet chosen by the user
  117. */
  118. void GetBet(int money, int& bet)
  119. {
  120. //int bet;
  121. if(( bet < money ) || ( bet > 0))
  122. {
  123. cout << " Enter bet : ";
  124. cin >> bet;
  125. }
  126. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 40
Reputation: student4lyfe is an unknown quantity at this point 
Solved Threads: 0
student4lyfe student4lyfe is offline Offline
Light Poster

Re: random number guess

 
0
  #19
Jul 26th, 2008
I'm get an error in my function that calls the program to keep count of the money error C2447: '{' : missing function header (old-style formal list?)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <ctime>
  7. using namespace std;
  8. /* ----------------------------------------------------------------- */
  9. /* Function Prototypes */
  10. /* ----------------------------------------------------------------- */
  11. void PrintHeading(int money); //prints header
  12. void GetBet(int money, int& bet); // gets bet
  13. int CalcNewMoney(int money, int bet, int numOfGuesses);
  14. /* ----------------------------------------------------------------- */
  15. int main ()
  16.  
  17. {
  18.  
  19. srand((unsigned)time(NULL)); // generates a random number
  20. int money = 1000; // beginning value for the money
  21. int num; // the number that the computer generates
  22. int bet; // the amount of money that the player bets
  23. int guess; // the player guess
  24. int numOfGuesses; // number of times the player guesses
  25. int sum=0; // total number of guesses
  26. int numofgamesplayed=0; // total number of games played
  27. int avgnumOfGuesses=0; // average number of games played
  28. int addedBalance = 0; // balance after money has been +-
  29. bool done;
  30. const int numMax = 7; // maximum number of guesses
  31. const int numMin = 0; // minimum number of guesses
  32. const int guessMax = 100; // max range of random number
  33. const int guessMin = 1; // min range of random number
  34. int choice =2 ;
  35.  
  36. PrintHeading(money);
  37.  
  38.  
  39.  
  40. do
  41. {
  42. GetBet( money, bet);
  43. num = (rand() + time(0)) % 100;
  44. done = false;
  45. ++numofgamesplayed;
  46. numOfGuesses = 1;
  47.  
  48. while ((numOfGuesses < 7) && (!done))
  49. {
  50. cout << " Guess " << numOfGuesses << " : ";
  51. cin >> guess;
  52. cout << endl;
  53. numOfGuesses++;
  54.  
  55. if (guess == num)
  56. {
  57. cout << " You guessed the correct number. " << endl << endl;
  58. done = true;
  59. }
  60. else
  61. if (guess < num)
  62. cout << " Your guess is lower than the number. " << endl << endl;
  63. else
  64. cout << " Your guess is higher than the number. " << endl << endl;
  65. }
  66.  
  67. sum+= numOfGuesses ;
  68. CalcNewMoney(money, bet, numOfGuesses);
  69. avgnumOfGuesses = sum/numofgamesplayed;
  70. cout << " Sorry... the correct answer was " << num << endl;
  71. cout << "\n Number of Games played "<<numofgamesplayed << endl;
  72. cout << " You have $ " << money << " dollars left " << endl;
  73. cout << "\n Total number of guesses :"<< sum;
  74. cout << "\n Guessing Average = :"<<avgnumOfGuesses;
  75. cout<< endl << " Like to try the game again < 1 or 2>? ";
  76. cin>> choice;
  77. }
  78. while( choice != 2);
  79.  
  80.  
  81.  
  82. return 0;
  83. }
  84. /*
  85. PrintHeading simply prints the introductory output.
  86. Parameters: initial amount of money received
  87. */
  88.  
  89. void PrintHeading(int money)
  90.  
  91. {
  92.  
  93.  
  94. cout << "=============================================" << endl;
  95. cout << " Welcome to the High Low betting Game. " << endl;
  96. cout << " You have $" << money << " to begin game. " << endl;
  97. cout << " Valid guesses are number between 1 and 100. " << endl;
  98. cout << "=============================================" << endl << endl;
  99.  
  100. }
  101. /*
  102. GetBet prompts for and reads in a bet. The function performs all
  103. error checking necessary to insure that a valid bet is read in
  104. and does not return until a valid bet is entered.
  105. Parameters:
  106. money: the amount of money the player currently has
  107. bet: the bet chosen by the user
  108. */
  109. void GetBet(int money, int& bet)
  110. {
  111. if(( bet < money ) || ( bet > 0))
  112. {
  113. cout << " Enter bet : ";
  114. cin >> bet;
  115. }}
  116. /*
  117. CalcNewMoney determines the amount of money the player has won or
  118. lost during the last game.
  119. Parameters:
  120. money: the amount of money the player had going into the game
  121. bet: the amount the player bet on the current game
  122. guesses: the number of guesses it took the player to win.
  123. -1 if the player did not guess correctly
  124. Return Value: the new amount of money the player has
  125. */
  126. int CalcNewMoney(int money, int bet, int numOfGuesses);
  127. {
  128. if((bet > 0)&&( bet <= money))
  129. {
  130. if (guess == num)
  131. money = bet / guess;
  132. else
  133. money = money - bet;
  134. }}
  135. //if ((bet > money)|| (bet < 0))
  136. //cout << " Please enter a vaild bet " <<endl;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC