Solution: Add Guess Feedback
The main() function is a simple number guessing game where the program randomly selects a number between 1 and 10, and the user keeps guessing until they get it right.
srand(time(0));seeds the random number generator so that each run produces a different random number.int secret = rand() % 10 + 1;generates a random number between 1 and 10 and stores it insecret.Prompts the user with "Guess a number between 1 and 10:" and reads their input into
guess.The
whileloop continues running as long as the user’s guess does not equalsecret.If the guess is lower than the secret number, it prints "Too low! Try again:"
If the guess is higher, it prints "Too high! Try again:"
Then it takes another guess using
cin >> guess;.
When the correct number is guessed, the loop ends and the program prints "You got it! The number was [secret]."
Ends with
return 0;to indicate successful program completion.
Solution: Add Guess Feedback
The main() function is a simple number guessing game where the program randomly selects a number between 1 and 10, and the user keeps guessing until they get it right.
srand(time(0));seeds the random number generator so that each run produces a different random number.int secret = rand() % 10 + 1;generates a random number between 1 and 10 and stores it insecret.Prompts the user with "Guess a number between 1 and 10:" and reads their input into
guess.The
whileloop continues running as long as the user’s guess does not equalsecret.If the guess is lower than the secret number, it prints "Too low! Try again:"
If the guess is higher, it prints "Too high! Try again:"
Then it takes another guess using
cin >> guess;.
When the correct number is guessed, the loop ends and the program prints "You got it! The number was [secret]."
Ends with
return 0;to indicate successful program completion.