Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This Program is a high-Lo guessing game
- //David Hawkins
- #include <iostream>
- #include <string>
- #include <cstdlib>//this is nescessary for using random numbers
- using namespace std;
- int main()
- {
- while (true)
- {
- int x = 1; //this variable keeps track of your guesses and modifies the question prompt to match.
- string y; //this is the string that is modified for the prompt by variable "x".
- int number = rand() % 100 + 1; //this is the number to be guessed. It is random.
- int guess; //This variable holds the value of your guess.
- string cont; //This is the variable allowing you to play a more than one game.
- 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";
- while (x <= 5) // This loop limits the game to 5 attempts
- {
- switch (x) // This switch modifies "y" to match the attempt you are on
- {
- case 1:
- y = " first ";
- break;
- case 2:
- y = " second ";
- break;
- case 3:
- y = " third ";
- break;
- case 4:
- y = " fourth ";
- break;
- case 5:
- y = " fifth and final ";
- break;
- }
- cout << "What is your" << y << "guess? \n";
- cin >> guess;
- while (guess < 1 || guess > 100) //This loop ensures that you can only enter correct inputs.
- {
- cout << "USE CORRECT INPUTS\n\nWhat is your" << y << "guess?\n";
- cin >> guess;
- }
- if (guess == number)
- {
- cout << "CORRECT! You win!\n";
- break;
- }
- else if (guess < number)
- {
- cout << "Your guess is too low.\n";
- x += 1;
- }
- else if (guess > number)
- {
- cout << "Your guess is too high.\n";
- x += 1;
- }
- }
- if (x <= 5) //This is the end of game statement if you win the game.
- {
- while (true) //This loop is error handling
- {
- cout << "Would you like to play again?\nY or N\n";
- cin >> cont;
- if (cont == "n" || cont == "N")
- {
- cout << "Thank you for playing.\n";
- exit(0);//this is the command to exit the program
- }
- else if (cont == "Y" || cont == "y")
- {
- break;
- }
- else
- {
- cout << "\nUSE CORRECT INPUTS!\n\n";
- }
- }
- }
- else
- {
- cout << "Too many guesses!\nYOU LOSE!\n"; //I changed the statement if you lose the game
- cout << "Would you like to play again?\nY or N\n";
- cin >> cont;
- while (true)
- {
- if (cont == "n" || cont == "N")
- {
- cout << "Thank you for playing.\n";
- exit(0);
- }
- else if (cont == "Y" || cont == "y")
- {
- break;
- }
- else
- {
- cout << "\nUSE CORRECT INPUTS!\n\n";
- cin >> cont;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement