Advertisement
Limited_Ice

Guessing Game

Sep 18th, 2019
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. //This Program is a high-Lo guessing game
  2.  
  3. //David Hawkins
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <cstdlib>//this is nescessary for using random numbers
  8. using namespace std;
  9.  
  10.  
  11.  
  12.  
  13. int main()
  14. {
  15.    
  16.  
  17.     while (true)
  18.     {
  19.         int x = 1;      //this variable keeps track of your guesses and modifies the question prompt to match.
  20.         string y;       //this is the string that is modified for the prompt by variable "x".
  21.         int number = rand() % 100 + 1;  //this is the number to be guessed.  It is random.
  22.         int guess;      //This variable holds the value of your guess.
  23.         string cont;    //This is the variable allowing you to play a more than one game.
  24.  
  25.  
  26.         cout << "This is a simple guessing game.\n\nYou have 5 tries to guess the magic number\nit is between 1 and 100.\n\n";
  27.         while (x <= 5) //   This loop limits the game to 5 attempts
  28.         {
  29.             switch (x)  //  This switch modifies "y" to match the attempt you are on
  30.             {
  31.             case 1:
  32.                 y = " first ";
  33.                 break;
  34.             case 2:
  35.                 y = " second ";
  36.                 break;
  37.             case 3:
  38.                 y = " third ";
  39.                 break;
  40.             case 4:
  41.                 y = " fourth ";
  42.                 break;
  43.             case 5:
  44.                 y = " fifth and final ";
  45.                 break;
  46.             }
  47.  
  48.             cout << "What is your" << y << "guess?  \n";
  49.             cin >> guess;
  50.             while (guess < 1 || guess > 100)    //This loop ensures that you can only enter correct inputs.  
  51.             {
  52.                 cout << "USE CORRECT INPUTS\n\nWhat is your" << y << "guess?\n";
  53.                 cin >> guess;
  54.             }
  55.  
  56.             if (guess == number)
  57.             {
  58.                 cout << "CORRECT! You win!\n";
  59.                 break;
  60.             }
  61.             else if (guess < number)
  62.             {
  63.                 cout << "Your guess is too low.\n";
  64.                 x += 1;
  65.             }
  66.             else if (guess > number)
  67.             {
  68.                 cout << "Your guess is too high.\n";
  69.                 x += 1;
  70.             }
  71.            
  72.  
  73.         }
  74.         if (x <= 5)     //This is the end of game statement if you win the game.
  75.         {
  76.             while (true)    //This loop is error handling
  77.             {
  78.                 cout << "Would you like to play again?\nY or N\n";
  79.                 cin >> cont;
  80.  
  81.                 if (cont == "n" || cont == "N")
  82.                 {
  83.                     cout << "Thank you for playing.\n";
  84.                     exit(0);//this is the command to exit the program
  85.                 }
  86.                 else if (cont == "Y" || cont == "y")
  87.                 {
  88.                     break;
  89.                 }
  90.                 else
  91.                 {
  92.                     cout << "\nUSE CORRECT INPUTS!\n\n";
  93.                 }
  94.             }
  95.         }
  96.         else
  97.         {
  98.             cout << "Too many guesses!\nYOU LOSE!\n";       //I changed the statement if you lose the game
  99.             cout << "Would you like to play again?\nY or N\n";
  100.             cin >> cont;
  101.             while (true)
  102.             {
  103.                 if (cont == "n" || cont == "N")
  104.                 {
  105.                     cout << "Thank you for playing.\n";
  106.                     exit(0);
  107.                 }
  108.                 else if (cont == "Y" || cont == "y")
  109.                 {
  110.                     break;
  111.                 }
  112.                 else
  113.                 {
  114.                     cout << "\nUSE CORRECT INPUTS!\n\n";
  115.                     cin >> cont;
  116.                 }
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement