Using C++ Create a game call Guess That Number! The program has an unlimited num
ID: 3890636 • Letter: U
Question
Using C++
Create a game call Guess That Number! The program has an unlimited number of rounds. In each round the program selects a random number that the user has to guess.
In Round 1 the user has to guess a number less than 5 within 5 tries. If the correct number is not guessed in 5 tries than the game is over. If the correct number is guessed than the user moves onto the next round where he/she has to guess a number less than 10 within 5 tries. In each subsequent round the range of numbers is doubled (5, 10, 20, 40, etc.).
In order to program this game you will need to use if-else statements, loops and random number generation using the cmath and ctime libraries.
Output/Input:
---------------------------------- Welcome to Guess That Number! ----------------------------------
Round 1
Guess a number less than 5: 1
Incorrect! try again (4 tries remaining)
Guess a number less than 5: 2
Incorrect! try again (3 tries remaining)
Guess a number less than 5: 0
Incorrect! try again (2 tries remaining)
Guess a number less than 5: 3
Incorrect! try again (1 tries remaining)
Guess a number less than 5: 4 Correct! The number was 4
Round 2
Guess a number less than 10: 4
Incorrect! try again (4 tries remaining)
Guess a number less than 10: 6
Incorrect! try again (3 tries remaining)
Guess a number less than 10: 1
Incorrect! try again (2 tries remaining)
Guess a number less than 10: 2
Incorrect! try again (1 tries remaining)
Guess a number less than 10: 7
Correct! The number was 7
Round 3
Guess a number less than 20: 0
Incorrect! try again (4 tries remaining)
Guess a number less than 20: 1
Incorrect! try again (3 tries remaining)
Guess a number less than 20: 2
Incorrect! try again (2 tries remaining)
Guess a number less than 20: 3 I
ncorrect! try again (1 tries remaining)
Guess a number less than 20: 4
Incorrect! Out of guesses :(
Game Over!!!
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int userValue;
int chances = 5;
srand (time(NULL));
int guessNumber = rand() % 5 + 1;
cout<<"---------------------------------- Welcome to Guess That Number! ----------------------------------"<<endl;
cout<<"Guess a number less than 5: ";
cin >> userValue;
while(userValue != guessNumber&& chances> 0) {
chances--;
cout<<"Incorrect! try again ("<<chances<<" tries remaining)"<<endl;
cout<<"Guess a number less than 5: ";
cin >> userValue;
}
if( chances > 0) {
cout<<"Correct! The number was "<<userValue<<endl;
} else {
cout<<"Incorrect! Out of guesses :("<<endl;
cout<<"Game Over!!!"<<endl;
}
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.