Question

In: Computer Science

C++ The program implements the Number Guessing Game. However, in that program, the user is given...

C++

The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number.

Rewrite the program so that the user has no more than five tries to guess the correct number.

Your program should print an appropriate message, such as “You win!” or “You lose!”.

Solutions

Expert Solution

Code:

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<random>
using namespace std;
int main()
{
int i,tries=5,userGuess=0;/*Declaring variables*/
srand(time(0));
int num;/*Generating random number from 1 to 10*/
num=rand()%(10)+1;
for(i=0;i<tries;i++)
{/*This loop iterated 5 times 0 to 4 that means 5 tries*/
cout<<"Guessn a number from (1-10):";
cin>>userGuess;
/*Rading user guessed number*/
if(userGuess==num)
{/*If user guesses corectly*/
break;
}
else
{/*If user doesn't guess correct*/
cout<<"Wrong Guess Try Again"<<endl;
}
  
}
if(i==tries)
{/*After the loop if the loop iterate 5 times that means
user didn't guess correct*/
cout<<"You Lose!";
}
else
{
/*If he guessed correctly*/
cout<<"You Win!"<<endl;
}
  
}

Output:

Indentation:

The code below will tell the user if the number is too high or too low

Code:

#include<iostream>
#include<random>
using namespace std;
int main()
{
   int i,tries=5,userGuess=0;/*Declaring variables*/
   int num=rand()%(10)+1;/*Generating random number from 1 to 10*/
   for(i=0;i<tries;i++)
   {/*This loop iterated 5 times 0 to 4 that means 5 tries*/
       cout<<"Guessn a number from (1-10):";
       cin>>userGuess;
       /*Rading user guessed number*/
       if(userGuess==num)
       {/*If user guesses corectly*/
           break;
       }
       else if(userGuess>num)
       {
           cout<<"Your Guess is Too High Try again"<<endl;
       }
       else if(userGuess<num)
       {
           cout<<"Your Guess is Too Low Try again"<<endl;
       }
      
   }
   if(i==tries)
   {/*After the loop if the loop iterate 5 times that means
       user didn't guess correct*/
       cout<<"You Lose!";
   }
   else
   {
       /*If he guessed correctly*/
       cout<<"You Win!"<<endl;
   }
  
}


Related Solutions

Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 rand = random.randint(min_number, max_number) #Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") while (True): #While loop, comparing the users guessed number with the random number. #If it matches, it will say you guessed it. guess = eval(input("Please try to guess my...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
Python3  Please add comments Write a program that implements the word guessing game - There is a...
Python3  Please add comments Write a program that implements the word guessing game - There is a secret word - The user enters letters one at a time, if the letter appears in the secret word, it is revealed. Otherwise, it counts as a wrong guess. If the user reveals all the letters in the word before getting too many wrong guesses then they win! Otherwise, they lose. 1 - define secret word 2 - create a revealed letter list that...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Using pseudocode design a number guessing game program. The program should generate a random number and...
Using pseudocode design a number guessing game program. The program should generate a random number and then ask the user to guess the number. Each time the user enters his or her guess, the program should indicate it was too high or too low. The game is over when the user correctly guesses the number. When the game ends, the program should display the number of guesses that the user made.
Java guessing game: For this program you will use Linear Search. - Ask the user to...
Java guessing game: For this program you will use Linear Search. - Ask the user to choose between a number from 0 to 2000. - The user will guess how long the linear search will take in nanoseconds, - After the user puts in their answer, show the user the difference between the actual answer and their answer. (Example: The user puts in 5 nanoseconds. The actual time is 11 nanoseconds. The program will show 6 nanoseconds.) There should be...
in java Create simple number guessing game as follows. First, prompt the user to enter a...
in java Create simple number guessing game as follows. First, prompt the user to enter a min and max value. In your code, declare and assign a variable with a random integer in this range (inclusive). Then, ask the user to guess the number. Input the user's guess. And at the end output "Correct!" if the user's guess was right on spot, or "Sorry, the answer is not corrcet.” The number I was looking for was xxx. Replace xxx with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT