943,783 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1612
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 24th, 2008
0

random number guess

Expand Post »
I have a program for clas that requires me to write a random number guessing game. I completed the game but i have to use functions in the program and I am not to familiar with this. Can some one help me? Here is the code and here are the functions that I have to use
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. int num;
  10. int guess;
  11. bool done;
  12. int noOfGuesses=0;
  13. int ncount=0;
  14. int sum=0;
  15. int noofgamesplayed=0;
  16. int avgNoOfGuesses=0
  17.  
  18. ;sum+=noOfGuesses;
  19. avgNoOfGuesses=sum/noofgamesplayed;
  20.  
  21. num = (rand() + time(0)) % 1000;
  22. done = false;
  23. while ((noOfGuesses < 10) && (!done))
  24.  
  25. {
  26. cout << "Enter an integer greater"
  27. << " than or equal to 0 and "
  28. << "less than 1000: ";
  29. cin >> guess;
  30. cout << endl;
  31. noOfGuesses++;
  32. if (guess == num)
  33. {
  34. cout << "you guessed the correct "
  35. << "number." << endl;
  36. done = true;
  37. }
  38. else
  39. if (guess < num)
  40. cout << "Your guess is lower "
  41. << "than the number. \n"
  42. << "Guess again!" << endl;
  43. else
  44. cout << "Your guess is higher "
  45. << "than the number.\n"
  46. << "guess again!" << endl;
  47. cout <<"Total gueses equal " << noOfGuesses << endl;
  48.  
  49. }
  50. return 0;
  51. }
These are the functions
c++ Syntax (Toggle Plain Text)
  1. /*
  2. PrintHeading simply prints the introductory output.
  3. Parameters: initial amount of money received
  4. */
  5. void PrintHeading(int money)
  6. /*
  7. GetBet prompts for and reads in a bet. The function performs all
  8. error checking necessary to insure that a valid bet is read in
  9. and does not return until a valid bet is entered.
  10. Parameters:
  11. money: the amount of money the player currently has
  12. bet: the bet chosen by the user
  13. */
  14. void GetBet(int money, int& bet);
  15. /*
  16. GetGuess reads in a guess. The user is not prompted for the guess in
  17. this function. The user only gets one chance to input a guess value.
  18. Return Value: the value of the guess if the input is valid
  19. 0 if the input guess was not valid
  20. */
  21. int GetGuess(void);
  22. /*
  23. CalcNewMoney determines the amount of money the player has won or
  24. lost during the last game.
  25. Parameters:
  26. money: the amount of money the player had going into the game
  27. bet: the amount the player bet on the current game
  28. guesses: the number of guesses it took the player to win.
  29. -1 if the player did not guess correctly
  30. Return Value: the new amount of money the player has
  31. */
  32. int CalcNewMoney(int money, int bet, int guesses);
  33. /*
  34. PlayAgain prompts the user to play the game again and reads in a response,
  35. using a single character to represent a yes or no reply.
  36. Error checking is performed on that response.
  37. Return Value: 1 if the user wants to play again
  38. 0 if the user does not want to play again.
  39. */
  40. int PlayAgain(void);
  41. /*
  42. PlayGame plays a single game, performing all the necessary calculations,
  43. input, and output.
  44. Parameters:
  45. money: the amount of money the player has at the start of the game.
  46. Return Value: how much the player has after the game.
  47. */
  48. int PlayGame(int money);
  49. /*
  50. Generates a random number between 1 and MAX_RANDOM_NUM, inclusive.
  51. Return Value: the generated number
  52. */
  53. int GenerateRandomNumber(void)
  54. { /* GenerateRandomNumber */
  55. /* calculate and return a number in the required range */
  56. return(rand() * MAX_RANDOM_NUM / RAND_MAX + 1);
  57. } /* GenerateRandomNumber */
  58. srand((unsigned)time(NULL));
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 24th, 2008
0

Re: random number guess

Have you compiled and run this?

This:
C++ Syntax (Toggle Plain Text)
  1. avgNoOfGuesses=sum/noofgamesplayed;
seems like it would generate an error, since noofgamesplayed is 0, and you're dividing by it. Besides, that statement and the one above it are unnecessary.

Do you need to write the functions, or do you simply need to call them? Either way, the functions seems to have a betting system that your current game doesn't implement.
Last edited by CoolGamer48; Jul 24th, 2008 at 9:38 pm. Reason: grammar
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 24th, 2008
0

Re: random number guess

i just compiled it and I found the error. Here is the code that I got to work:

c++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. #include <iostream>
  4.  
  5. #include <fstream>
  6.  
  7. #include <string>
  8.  
  9. #include <cstdlib>
  10.  
  11. #include <iomanip>
  12.  
  13. #include <ctime>
  14.  
  15. using namespace std;
  16.  
  17.  
  18. int main ()
  19.  
  20. {
  21.  
  22. int num;
  23.  
  24. int guess;
  25.  
  26. bool done;
  27.  
  28. int numOfGuesses;
  29.  
  30. int ncount=0;
  31.  
  32. int sum=0;
  33.  
  34. int numofgamesplayed=0;
  35.  
  36. int avgnumOfGuesses=0;
  37.  
  38. char choice ;
  39.  
  40. char n;
  41.  
  42. char y;
  43.  
  44. cout << "=============================================" << endl;
  45.  
  46. cout << " Welcome to the High Low betting Game. " << endl;
  47.  
  48. cout << " You have a $1000 to begin game. " << endl;
  49.  
  50. cout << " Valid guesses are number between 1 and 100. " << endl;
  51.  
  52. cout << "=============================================" << endl << endl;
  53.  
  54. do
  55.  
  56. {
  57.  
  58. num = (rand() + time(0)) % 100;
  59.  
  60. done = false;
  61.  
  62. ++numofgamesplayed;
  63.  
  64. numOfGuesses = 1;
  65.  
  66. while ((numOfGuesses < 7) && (!done))
  67.  
  68. {
  69.  
  70. cout << " Guess " << numOfGuesses << " : ";
  71.  
  72. cin >> guess;
  73.  
  74. cout << endl;
  75.  
  76. numOfGuesses++;
  77.  
  78. if (guess == num)
  79.  
  80. {
  81.  
  82. cout << " You guessed the correct number. " << endl << endl;
  83.  
  84. done = true;
  85.  
  86. }
  87.  
  88. else
  89.  
  90. if (guess < num)
  91.  
  92. cout << " Your guess is lower than the number. " << endl << endl;
  93.  
  94. else
  95.  
  96. cout << " Your guess is higher than the number. " << endl << endl;
  97.  
  98. }
  99.  
  100. sum+= numOfGuesses ;
  101.  
  102. cout << " Sorry... the correct answer was " << num << endl;
  103.  
  104. cout<<"\n Number of Games played "<<numofgamesplayed;
  105.  
  106. cout<<"\n Total number of guesses :"<< sum;
  107.  
  108. cout<<"\n Guessing Average = :"<<sum/numofgamesplayed;
  109.  
  110. }
  111.  
  112. while( choice != y);
  113.  
  114. cout<< endl << " Like to try the game again <y or n>? ";
  115.  
  116. cin>> choice;
  117.  
  118. return 0;
  119.  
  120. }
I know i wasn't able to get the beting system to work.
Last edited by student4lyfe; Jul 24th, 2008 at 9:55 pm. Reason: answer the last part of the response to my question
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 24th, 2008
0

Re: random number guess

I'll start you off with the first function, PrintHeading(int). This is simply taking lines 44 through 52 above, taking them out of main, and putting them into PrintHeading(int). In addition, the $1000 is no longer hard-coded in the cout statement, but instead now uses the parameter passed to the function. Lines 44 through 52 are replaced with the function call, and a function declaration is added before main. This is the easiest of your functions, but hopefully it'll help. Give the others a try. Also, not sure if you noticed, but your srand line was accidentally cut off from your program when you were splitting the program in two in your first post. Not sure where it was originally, but I found it at the bottom of the functions code in your first post and put it at the top of main.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3.  
  4. #include <fstream>
  5.  
  6. #include <string>
  7.  
  8. #include <cstdlib>
  9.  
  10. #include <iomanip>
  11.  
  12. #include <ctime>
  13.  
  14. using namespace std;
  15.  
  16.  
  17. void PrintHeading(int money);
  18.  
  19.  
  20. int main ()
  21.  
  22. {
  23.  
  24. srand((unsigned)time(NULL));
  25.  
  26. int num;
  27.  
  28. int guess;
  29.  
  30. bool done;
  31.  
  32. int numOfGuesses;
  33.  
  34. int ncount=0;
  35.  
  36. int sum=0;
  37.  
  38. int numofgamesplayed=0;
  39.  
  40. int avgnumOfGuesses=0;
  41.  
  42. char choice ;
  43.  
  44. char n;
  45.  
  46. char y;
  47.  
  48. PrintHeading (1000);
  49.  
  50. do
  51.  
  52. {
  53.  
  54. num = (rand() + time(0)) % 100;
  55.  
  56. done = false;
  57.  
  58. ++numofgamesplayed;
  59.  
  60. numOfGuesses = 1;
  61.  
  62. while ((numOfGuesses < 7) && (!done))
  63.  
  64. {
  65.  
  66. cout << " Guess " << numOfGuesses << " : ";
  67.  
  68. cin >> guess;
  69.  
  70. cout << endl;
  71.  
  72. numOfGuesses++;
  73.  
  74. if (guess == num)
  75.  
  76. {
  77.  
  78. cout << " You guessed the correct number. " << endl << endl;
  79.  
  80. done = true;
  81.  
  82. }
  83.  
  84. else
  85.  
  86. if (guess < num)
  87.  
  88. cout << " Your guess is lower than the number. " << endl << endl;
  89.  
  90. else
  91.  
  92. cout << " Your guess is higher than the number. " << endl << endl;
  93.  
  94. }
  95.  
  96. sum+= numOfGuesses ;
  97.  
  98. cout << " Sorry... the correct answer was " << num << endl;
  99.  
  100. cout<<"\n Number of Games played "<<numofgamesplayed;
  101.  
  102. cout<<"\n Total number of guesses :"<< sum;
  103.  
  104. cout<<"\n Guessing Average = :"<<sum/numofgamesplayed;
  105.  
  106. }
  107.  
  108. while( choice != y);
  109.  
  110. cout<< endl << " Like to try the game again <y or n>? ";
  111.  
  112. cin>> choice;
  113.  
  114. return 0;
  115.  
  116. }
  117.  
  118.  
  119.  
  120.  
  121. /*
  122. PrintHeading simply prints the introductory output.
  123. Parameters: initial amount of money received
  124. */
  125. void PrintHeading(int money)
  126. {
  127. cout << "=============================================" << endl;
  128. cout << " Welcome to the High Low betting Game. " << endl;
  129. cout << " You have $" << money << " to begin game. " << endl;
  130. cout << " Valid guesses are number between 1 and 100. " << endl;
  131. cout << "=============================================" << endl << endl;
  132. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Jul 25th, 2008
0

Re: random number guess

Ok, this is what I wrote for my next function but I am getting an error saying that bet is not declared. Can someone tell me what I am doing wrong?
c++ Syntax (Toggle Plain Text)
  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));
  19. int money = 1000;
  20. int num;
  21.  
  22. int guess;
  23. int numOfGuesses;
  24. int ncount=0;
  25. int sum=0;
  26. int numofgamesplayed=0;
  27. int avgnumOfGuesses=0;
  28. bool done;
  29. const int numMax = 7;
  30. const int numMin = 0;
  31. const int guessMax = 100;
  32. const int guessMin = 1;
  33. char choice ;
  34. //char n;
  35. char y;
  36. GetBet( money, bet);
  37. PrintHeading(money);
  38. do
  39. {
  40. num = (rand() + time(0)) % 100;
  41. done = false;
  42. ++numofgamesplayed;
  43. numOfGuesses = 1;
  44.  
  45. while ((numOfGuesses < 7) && (!done))
  46. {
  47. cout << " Guess " << numOfGuesses << " : ";
  48. cin >> guess;
  49. cout << endl;
  50. numOfGuesses++;
  51.  
  52. if (guess == num)
  53. {
  54. cout << " You guessed the correct number. " << endl << endl;
  55. done = true;
  56. }
  57. else
  58. if (guess < num)
  59. cout << " Your guess is lower than the number. " << endl << endl;
  60. else
  61. cout << " Your guess is higher than the number. " << endl << endl;
  62. }
  63. sum+= numOfGuesses ;
  64. cout << " Sorry... the correct answer was " << num << endl;
  65. cout<<"\n Number of Games played "<<numofgamesplayed;
  66. cout<<"\n Total number of guesses :"<< sum;
  67. cout<<"\n Guessing Average = :"<<sum/numofgamesplayed;
  68. }
  69. while( choice != y);
  70. cout<< endl << " Like to try the game again <y or n>? ";
  71. cin>> choice;
  72.  
  73. return 0;
  74. }
  75. /*
  76. PrintHeading simply prints the introductory output.
  77. Parameters: initial amount of money received
  78. */
  79.  
  80. void PrintHeading(int money)
  81.  
  82. {
  83.  
  84.  
  85. cout << "=============================================" << endl;
  86. cout << " Welcome to the High Low betting Game. " << endl;
  87. cout << " You have $" << money << " to begin game. " << endl;
  88. cout << " Valid guesses are number between 1 and 100. " << endl;
  89. cout << "=============================================" << endl << endl;
  90.  
  91. }
  92. /*
  93. GetBet prompts for and reads in a bet. The function performs all
  94. error checking necessary to insure that a valid bet is read in
  95. and does not return until a valid bet is entered.
  96. Parameters:
  97. money: the amount of money the player currently has
  98. bet: the bet chosen by the user
  99. */
  100. void GetBet(int money, int& bet);
  101. {
  102. int bet;
  103. while (( bet <= money ) && ( bet > 0))
  104. cout << " Enter bet : " << bet;
  105. cin >> bet;
  106. }
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 25th, 2008
0

Re: random number guess

Here are your variable declarations in main.
C++ Syntax (Toggle Plain Text)
  1. int money = 1000;
  2. int num;
  3.  
  4. int guess;
  5. int numOfGuesses;
  6. int ncount=0;
  7. int sum=0;
  8. int numofgamesplayed=0;
  9. int avgnumOfGuesses=0;
  10. bool done;
  11. const int numMax = 7;
  12. const int numMin = 0;
  13. const int guessMax = 100;
  14. const int guessMin = 1;
  15. char choice ;
  16. //char n;
  17. char y;

bet is not among them, so when the compiler gets to this line, it doesn't know what bet is.

C++ Syntax (Toggle Plain Text)
  1. GetBet( money, bet);

Note that this line at the top doesn't allow you to use bet in main:

void GetBet(int money, int& bet);

That's a function declaration that lets you use the GetBet function, not the variable bet.

Nor does the fact that you declare bet later in the GetBet function itself allow you to use bet in main. You need to add this line:

C++ Syntax (Toggle Plain Text)
  1. int bet;

inside main before you use this line:

C++ Syntax (Toggle Plain Text)
  1. GetBet( money, bet);
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Jul 25th, 2008
0

Re: random number guess

I was told that you are suppose to declare your variables in your functions
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 25th, 2008
0

Re: random number guess

I was told that you are suppose to declare your variables in your functions
That's a very broad statement. main IS a function so if you are going to use a variable in main and you don't want to declare it globally, which you usually don't, and you haven't done so here, you need to declare it in main.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Jul 25th, 2008
0

Re: random number guess

also itried what you said and still got an error. Imust be missing a step.
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 25th, 2008
0

Re: random number guess

also itried what you said and still got an error. Imust be missing a step.
I said you had an error. I didn't say that was the only error. You need to state what the error is rather than just say you have an error.

At minimum you have two errors in this function:

	void GetBet(int money, int& bet);
	{
		int bet;
		while (( bet <= money ) && ( bet > 0))
		cout << " Enter bet : " << bet;
		cin >> bet;
	}

One, the function declaration has a semicolon at the end of it, but not the function itself. Delete the semicolon in red. Two, You've already declared bet in the function parameters, so don't do it in the function itself. Delete int bet; here.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to know when a window of another process is activated
Next Thread in C++ Forum Timeline: Flow charts for currency conversion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC