By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Help Please in making Code replay the game.

P: 1
Hi guys I'm new to C++ Programming and I am having trouble in making my Guessing game code replay. I am suppose to give the user that is playing my game the opportunity to play again with options of 'Yes' and 'No' and if Yes then the game starts up again and if No then the game tells the user bye and quits.

"Do you want to play again? (y/n)"

Please Help if possible thanks. ^^


-----------------------------------------

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() 
  6. {
  7.     cout << "\t\tWelcome to the Number Guessing Game!\n\n";
  8.  
  9.     cout << "Think of a number between 1 and 1000 and I'll try to guess it.\n"; 
  10.  
  11.     cout << "After guess, respond with 1 for higher, 2 for lower, or 3 for correct";
  12.  
  13.  
  14.     int tries = 0;
  15.     int low = 0;
  16.     int high = 1001;
  17.     char response = 1;
  18.  
  19.  
  20.     do
  21.     {
  22.          int guess = (low + high) / 2;
  23.          cout << "\nMy guess is " << guess << endl;
  24.          tries += 1;
  25.  
  26.  
  27.     cout << "Is your number (1)higher, (2) lower or am I correct?(3): ";
  28.     cin >> response;
  29.  
  30.         switch (response)
  31.         {
  32.         case '1':
  33.             low = guess;
  34.             break;
  35.         case '2':
  36.              high = guess;
  37.              break;
  38.         case '3':
  39.              cout << "Yeah! I guessed your number in only " << tries << " tries.";
  40.              break;
  41.         default:
  42.             cout << "Uh, error. You made an illegal choice. " << response << endl;
  43.         } 
  44.     } while (response != '3');
  45.  
  46.  
  47.     system ("pause");
  48.  
  49.     return 0;
  50. }
Feb 24 '08 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
You need a loop in main() that tests for the choice to continue or not.

If the choice is to continue, then call the code you have already written. Your main() is really the game. So change the name to Game().

If the choice is to quit, then break out of the loop.
Feb 24 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.