Number Game

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

Join Date: Aug 2008
Posts: 22
Reputation: FtKShadow is an unknown quantity at this point 
Solved Threads: 0
FtKShadow FtKShadow is offline Offline
Newbie Poster

Number Game

 
0
  #1
Oct 14th, 2008
Alright I have been working on this game for like a week or 2 now, but I am having some trouble towards the end.

Heres the code I have gotten so far:

  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6. char play;
  7. int guess;
  8. bool done;
  9. int noOfGuesses = 0;
  10. int random;
  11. int c;
  12. double bank, winnings, total;
  13. int check;
  14.  
  15. void printPayout()
  16. {
  17. cout << "1 guess, win 2.00" << endl;
  18. cout << "2 guesses, win 1.75" << endl;
  19. cout << "3 guesses, win 1.5" << endl;
  20. cout << "4 guesses, win 1.25" << endl;
  21. cout << "5 guesses, win 1.00" << endl;
  22. cout << "6 guesses, .75" << endl;
  23. cout << "7 guesses, win .5" << endl;
  24. cout << "8 guesses, win .25" << endl << endl;
  25. }
  26.  
  27. int checkGuess(int guess)
  28. {
  29. if (guess > random)
  30. {
  31. check = 1;
  32. }
  33. else if (guess < random)
  34. {
  35. check = -1;
  36. }
  37. else
  38. {
  39. check = 0;
  40. }
  41.  
  42. return (check);
  43. }
  44.  
  45. int genRandom()
  46. {
  47. int r;
  48. srand(time(NULL));
  49. r = rand() % 100 + 1;
  50.  
  51. return r;
  52. }
  53.  
  54.  
  55. int game()
  56. {
  57. cout << "Welcome to the guess-o-matic. It only costs a dollar to play. You could double your bet." << endl << endl;
  58. cout << "Do you want to play (y / n)" << endl;
  59. bank = 100.00;
  60. cin >> play;
  61. if (play=='y')
  62. {
  63. winnings = 2.00;
  64. cout << "Great!, your payout will be as follows:" << endl << endl;
  65. printPayout();
  66.  
  67. random = genRandom();
  68. while((bank!=0)&&(play=='y'))
  69. {
  70. noOfGuesses=0;
  71. random = genRandom();
  72. winnings = 2.00;
  73.  
  74. while ((noOfGuesses!=8))
  75. {
  76. cout << "Now guess a number between 1 and 100" << endl;
  77. cin >> guess;
  78. noOfGuesses++;
  79. c = checkGuess (guess);
  80. if (c == 0)
  81. {
  82. bank = bank + winnings;
  83. cout << "Correct, you win " << winnings << ", your bank is now "<< bank<< endl<<"Do You Wanna Play Again??(Y/N)";
  84. cin>>play;
  85. break;
  86. }
  87. else if (c == -1)
  88. {
  89. cout << "Sorry too low try higher" << endl;
  90. winnings = winnings - .25;
  91. }
  92. else if (c == 1){
  93. cout << "Sorry too high try lower" << endl;
  94. winnings = winnings - .25;
  95. }
  96. }
  97. }
  98. }
  99. cout<<"Thank You For Playing, you are left with a total of " <<bank<<" $\n Wishing you will be back to play once more.";
  100. return 0;
  101. }
  102.  
  103.  
  104. int main()
  105. {
  106.  
  107. game();
  108.  
  109. return 0;
  110. }

I am missing this part I just realized and dont know how I can take it out and make it a function.

4. You must have a function called calcWinnings that takes as an argument, the number of guesses it took to determine the number. You must use a switch statement to determine the winnings. This function returns the number of winnings.

I also realized it doesn't take a dollar away when you lose.

another part I am also missing is

3. You must ask the user if they want to continue after each correct guess. This means they cannot quit in the middle of a game
Anyone know of a good way to add these. I have been trying and keep doing something wrong. If someone could help me out here I would be so thankful
Last edited by FtKShadow; Oct 14th, 2008 at 4:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Number Game

 
0
  #2
Oct 14th, 2008
The wording in 4) suggests that you don't need to calculate the winnings on the fly lines 90 and 94. Keep track of the number of guesses and pass that value to a function called calcWinnings() which is called on line 81 . Use the value passed in as the conditional in the switch statement. The switch statement will look a lot like the printPayout function except that it will use case statements indicating what the winnings are based on the value passed in. The return value of the function can be stored in the variable winnings and that value added to the bank.

Except for possible case sensitivity considerations it looks like you already have 3) completed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 22
Reputation: FtKShadow is an unknown quantity at this point 
Solved Threads: 0
FtKShadow FtKShadow is offline Offline
Newbie Poster

Re: Number Game

 
0
  #3
Oct 14th, 2008
Originally Posted by Lerner View Post
The wording in 4) suggests that you don't need to calculate the winnings on the fly lines 90 and 94. Keep track of the number of guesses and pass that value to a function called calcWinnings() which is called on line 81 . Use the value passed in as the conditional in the switch statement. The switch statement will look a lot like the printPayout function except that it will use case statements indicating what the winnings are based on the value passed in. The return value of the function can be stored in the variable winnings and that value added to the bank.

Except for possible case sensitivity considerations it looks like you already have 3) completed.
alright the only problem I noticed I am having now is when they lose having it take a dollar away go back to the loop so it asks them if they want to play again.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Number Game

 
0
  #4
Oct 14th, 2008
Add another conditional to the inner while loop. The inner loop should continue if the number of guesses is less than 8 and the correct number hasn't been guessed. Use a flag to keep track of whether the correct number has been guessed, maybe a boolean variable called notguessed set to default true each new "game" and changed to false if c == 0.

Move everything except changing the notguessed flag and the break statement in the if c == 0 scenario outside of the inner while loop and place it after the inner while loop and before the restart of the outer while loop.

Determine why the inner file loop stopped. If it was because the correct answer was guessed then output congratulatory response. If it was because the correct value wasn't guessed in 8 tries, then deduct the appropriate amount from the appropriate variable.

In either case indicate the current "winning" or "bank" value or whatever and ask if they want to play another game.

Be sure to set all necessary flags and counters to the appropriate default values before each game is played.
Last edited by Lerner; Oct 14th, 2008 at 10:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 32
Reputation: emotionalone is an unknown quantity at this point 
Solved Threads: 4
emotionalone emotionalone is offline Offline
Light Poster

Re: Number Game

 
0
  #5
Oct 14th, 2008
May you please PM me the entire assignment? I like these kind of game problems ^^. I would ask for you to post it but I don't think that's what you're here for. It may also help me understand the problem more in depth and help if I can.
Last edited by emotionalone; Oct 14th, 2008 at 11:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 22
Reputation: FtKShadow is an unknown quantity at this point 
Solved Threads: 0
FtKShadow FtKShadow is offline Offline
Newbie Poster

Re: Number Game

 
0
  #6
Oct 15th, 2008
Originally Posted by emotionalone View Post
May you please PM me the entire assignment? I like these kind of game problems ^^. I would ask for you to post it but I don't think that's what you're here for. It may also help me understand the problem more in depth and help if I can.
Well I finished and turned it in, but if you want to take a swing at it.

http://www.glennstevenson.com/c++onl...8/midterm.html
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC