Advertisement
Guest User

Modified Guess

a guest
May 27th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     cout << "\tWelcome to Guess My Number!\n\n";
  11.  
  12.     // Initialization that is done before the game is started
  13.     char playAgain = 'y';
  14.     int level;
  15.     srand(time(0)); // seed the random number generator - we only need to do this once
  16.  
  17.     const int NUM_LEVELS = 4;
  18.     int levelRanges[NUM_LEVELS] = {100, 1000, 5000, 25000};
  19.  
  20.     do
  21.     {
  22.     cout << "Difficulty Selection: \n\n";
  23.     cout << "1 - Easy\n2 - Normal\n3 - Hard\n4 - Unbeatable\n\n";
  24.     cin >> level;
  25.     cout << "\nYou chose level " << level << "\n\n";
  26.  
  27.     // NOTE:  DOES NOT CHECK ARRAY BOUNDARIES!
  28.     int theNumber = rand() % levelRanges[level-1] + 1; // random number between 1 and the specified range
  29.     int tries = 0, guess;
  30.  
  31.     do
  32.     {
  33.         cout << "Enter a guess: ";
  34.  
  35.         string str;
  36.         cin >> str;
  37.         istringstream iss(str);
  38.         iss >> guess ;
  39.         if (iss.eof() == false)
  40.         cout << "\n\nPlease enter only numbers.\n\n";
  41.         else
  42.         {
  43.  
  44.         ++tries;
  45.  
  46.         if (guess > theNumber)
  47.             cout << "Too high!\n\n";
  48.  
  49.         if (guess < theNumber)
  50.             cout << "Too low!\n\n";
  51.         }
  52.  
  53.     } while (guess != theNumber);
  54.  
  55.     cout << "\nThat's it! You got it in " << tries << " guesses!\n";
  56.  
  57.     cout << "Play again? (y/n)\n";
  58.     cin >> playAgain;
  59.  
  60.     } while (playAgain == 'y');
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement