Advertisement
Guest User

guess.cpp

a guest
Jan 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. /*
  2.     albert874599_guess.cpp
  3.     g++ guess.cpp -o guess -Wall -std=c++ll
  4.     Albert Brandt
  5. */
  6.  
  7. #include <iostream>
  8. #include <cstdlib>
  9. #include <ctime>
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     /*
  16.     Declaring variables, secret is the number that is being guessed, guess is what the user guesses, lives is the amount of chances the user has left to guess.
  17.     */
  18.     int secret, guess;
  19.     int lives = 9;
  20.    
  21.     /*
  22.     srand sets a random gen seed based on the time.
  23.     rand() gets a random number.
  24.     */
  25.     srand(time(0));
  26.     secret = rand() % 10 + 1;
  27.  
  28.     // Keep looping through this block of code if the guess is not the secret and if the user has lives left.
  29.     while (guess != secret and lives > 0)
  30.     {
  31.         cout << "Enter an integer greater than or equal to 1 and less than 10: ";
  32.         cin >> guess;
  33.         cout << endl;
  34.        
  35.         // If user guesses correctly then execute everything in the brackets, else if the user guesses too low then execute the code saying the guess is too low, and opposite goes for the next else statement.
  36.         if (guess == secret)
  37.         {
  38.             cout << "You guessed the correct number, go figure." << endl;
  39.             cout << endl;
  40.         }
  41.         else if (guess < secret)
  42.         {
  43.             cout << "Your guess is lower than the number.. Guess again!" << endl;
  44.             // Subtract a life from the user's lives.
  45.             lives--;
  46.             cout << endl;
  47.         }
  48.         else
  49.         {
  50.             cout << "Your guess is higher than the number.. Try guessing a lower number.. Like your IQ." << endl;
  51.             lives--;
  52.             cout << endl;
  53.         }
  54.  
  55.     }
  56.    
  57.     // IF the user has no more lives left then tell them the secret number and end the game.
  58.     if (lives <= 0)
  59.     {
  60.         cout << "So sorry that you suck. No more chances!" << endl;
  61.         cout << "The secret was " << secret << endl;
  62.         cout << endl;
  63.     }
  64.    
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement