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...
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.
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 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." If the user guesses the number, the application should congratulate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT