Using C++.. *** I already have some sort of code, if you could help me with it,
ID: 3890751 • Letter: U
Question
Using C++..
*** I already have some sort of code, if you could help me with it, it would be much appreciated, thank you! *** Link: https://pastebin.com/V2KvmKeY
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!!!
*** I already have some sort of code, if you could help me with it, it would be much appreciated, thank you! *** Link: https://pastebin.com/V2KvmKeY
Explanation / Answer
//Tested on windows
/**************************game.cpp*****************************************/
#include <iostream>
#include <ctime>
#include <cstdlib>
const int LOWER_NUMBER_BOUND = 0;
const int UPPER_NUMBER_BOUND = 9;
const int GUESS_ATTEMPTS = 5;
// Here's what my program looks like:
int main()
{
int userGuess;
int numberToGuess;
int guessAttemptsRemaining;
int playAgain = 0;
int round = 1; // initial round number
int roundValue = 5; // intial round value
srand(time(0));
system("CLS"); // clear screen
std::cout << "------------------------------ ";
std::cout << "Welcome to Guess that number! ";
guessAttemptsRemaining = GUESS_ATTEMPTS; // initial setting guess attempt
numberToGuess = rand() % roundValue;// number to guess
do
{
std::cout << "------------------------------ ";
std::cout << "Round "<<round<<". ";
userGuess = -1;
while (userGuess < 0 || userGuess > roundValue)
{
std::cout << "Guess a number less than "<<roundValue<<": ";
std::cin >> userGuess;
std::cout<<"Entered: "<<userGuess<<std::endl;
if (userGuess < 0 || userGuess > roundValue)
{
std::cin.clear();
std::cin.ignore(10000, ' ');
}
}
--guessAttemptsRemaining;
std::cout << "You picked: " << userGuess << ". ";
if (userGuess != numberToGuess)
{
// std::cout << "Incorrect, try again ";
if (guessAttemptsRemaining > 0)
{
std::cout << "Incorrect, try again " << guessAttemptsRemaining << " guess(es) remaining. ";
}
else
{
std::cout << "Game Over ";
}
}
else
{
std::cout << "Correct! The number was "<<numberToGuess<<" ";
//round number, random boundary, random number, guessAttemptsRemaining need to change
round++;
roundValue = roundValue*2;
guessAttemptsRemaining = GUESS_ATTEMPTS;
numberToGuess = rand() % roundValue;
}
} while (guessAttemptsRemaining > 0);
return 0;
}
/*****************************output**************************************/
------------------------------
Welcome to Guess that number!
------------------------------
Round 1.
Guess a number less than 5: 2
Entered: 2
You picked: 2.
Correct! The number was 2
------------------------------
Round 2.
Guess a number less than 10: 8
Entered: 8
You picked: 8.
Incorrect, try again 4 guess(es) remaining.
------------------------------
Round 2.
Guess a number less than 10: 9
Entered: 9
You picked: 9.
Incorrect, try again 3 guess(es) remaining.
------------------------------
Round 2.
Guess a number less than 10: 4
Entered: 4
You picked: 4.
Incorrect, try again 2 guess(es) remaining.
------------------------------
Round 2.
Guess a number less than 10: 2
Entered: 2
You picked: 2.
Incorrect, try again 1 guess(es) remaining.
------------------------------
Round 2.
Guess a number less than 10: 6
Entered: 6
You picked: 6.
Correct! The number was 6
------------------------------
Round 3.
Guess a number less than 20: 12
Entered: 12
You picked: 12.
Incorrect, try again 4 guess(es) remaining.
------------------------------
Round 3.
Guess a number less than 20: 17
Entered: 17
You picked: 17.
Incorrect, try again 3 guess(es) remaining.
------------------------------
Round 3.
Guess a number less than 20: 9
Entered: 9
You picked: 9.
Correct! The number was 9
------------------------------
Round 4.
Guess a number less than 40: 37
Entered: 37
You picked: 37.
Incorrect, try again 4 guess(es) remaining.
------------------------------
Round 4.
Guess a number less than 40: 36
Entered: 36
You picked: 36.
Incorrect, try again 3 guess(es) remaining.
------------------------------
Round 4.
Guess a number less than 40: 26
Entered: 26
You picked: 26.
Incorrect, try again 2 guess(es) remaining.
------------------------------
Round 4.
Guess a number less than 40: 22
Entered: 22
You picked: 22.
Incorrect, try again 1 guess(es) remaining.
------------------------------
Round 4.
Guess a number less than 40: 15
Entered: 15
You picked: 15.
Game Over
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.