Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std; // allows you to not have to type "std::" in front of members of the std library
  7.  
  8. int generateRandomNumber(int randomMax) // generate a number between 1 and the max
  9. {
  10.     srand(time(NULL)); // "seed" the random so it has somethgin ever changign to go off (for example, the time), otherwise it'll just draw the same number every time.
  11.  
  12.     return (rand() % randomMax) + 1; // generate a random number between 1 and the random max usign modulus magic
  13. }
  14.  
  15. bool checkNumber(int randomNumber, int givenNumber) // checks if the number is correct or not
  16. {
  17.     if (givenNumber == randomNumber)
  18.     {
  19.         return true;
  20.     }
  21.     else
  22.     {
  23.         return false;
  24.     }
  25.  
  26.     return false;
  27. }
  28.  
  29. string higherOrLower(int randomNumber, int givenNumber) // checks if the number is correct or not
  30. {
  31.     if (givenNumber > randomNumber)
  32.     {
  33.         return "Your number was too high.\n";
  34.     }
  35.     else if (givenNumber < randomNumber)
  36.     {
  37.         return "Your number was too low.\n";
  38.     }
  39.  
  40.     return "Error <higherOrLower>";
  41. }
  42.  
  43. void main()
  44. {
  45.     const int RANDOM_MAX = 100;
  46.     const int PLAYER_TRIES = 10;
  47.  
  48.     string playerName = "";
  49.  
  50.     string getNumber = "";
  51.     int givenNumber = 0;
  52.  
  53.     int randomNum = 0;
  54.     int tries = 0;
  55.  
  56.     bool gamePlaying = true;
  57.  
  58.     randomNum = generateRandomNumber(RANDOM_MAX);
  59.  
  60.     cout << "Please enter your name: ";
  61.     getline(cin, playerName);
  62.  
  63.     cout << "Hi " << playerName << ", you'll be guessing a number between 1 and " << RANDOM_MAX
  64.         << ".\nYou have " << PLAYER_TRIES << " tries to get it right, good luck!" << endl;
  65.  
  66.     cout << randomNum << endl;
  67.  
  68.     while (gamePlaying == true)                                 // a while loop keeps goign while the condition in its argument is true.
  69.     {
  70.         if (tries < PLAYER_TRIES)
  71.         {
  72.             cout << "Enter guess (Try: " << tries + 1 << "): "; //{
  73.             getline(cin, getNumber);                            //  All of this gets the number and converts it to an integer for comparisons
  74.             givenNumber = stoi(getNumber);                      //}
  75.  
  76.             if (checkNumber(randomNum, givenNumber))
  77.             {
  78.                 cout << "You guessed the right number!" << endl;
  79.  
  80.                 gamePlaying = false;
  81.             }
  82.             else
  83.             {
  84.                 cout << higherOrLower(randomNum, givenNumber);
  85.                 tries += 1;
  86.             }
  87.         }
  88.         else
  89.         {
  90.             cout << "You ran out of tries, better luck next time!\n";
  91.             gamePlaying = false;
  92.         }
  93.     }
  94.  
  95.     system("pause");
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement