Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <sstream>
- using namespace std;
- int main()
- {
- cout << "\tWelcome to Guess My Number!\n\n";
- // Initialization that is done before the game is started
- char playAgain = 'y';
- int level;
- srand(time(0)); // seed the random number generator - we only need to do this once
- const int NUM_LEVELS = 4;
- int levelRanges[NUM_LEVELS] = {100, 1000, 5000, 25000};
- do
- {
- cout << "Difficulty Selection: \n\n";
- cout << "1 - Easy\n2 - Normal\n3 - Hard\n4 - Unbeatable\n\n";
- cin >> level;
- cout << "\nYou chose level " << level << "\n\n";
- // NOTE: DOES NOT CHECK ARRAY BOUNDARIES!
- int theNumber = rand() % levelRanges[level-1] + 1; // random number between 1 and the specified range
- int tries = 0, guess;
- do
- {
- cout << "Enter a guess: ";
- string str;
- cin >> str;
- istringstream iss(str);
- iss >> guess ;
- if (iss.eof() == false)
- cout << "\n\nPlease enter only numbers.\n\n";
- else
- {
- ++tries;
- if (guess > theNumber)
- cout << "Too high!\n\n";
- if (guess < theNumber)
- cout << "Too low!\n\n";
- }
- } while (guess != theNumber);
- cout << "\nThat's it! You got it in " << tries << " guesses!\n";
- cout << "Play again? (y/n)\n";
- cin >> playAgain;
- } while (playAgain == 'y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement