// Amari Mosley CSC5 Chapter 5, P. 298, #20
//
/**************************************************************
*
* COMPUTE RANDOM NUMBER GAME
*______________________________________________________________
* This program generates a random number and asks the user to
* guess it. It provides feedback ("Too high, try again" or
* "Too low, try again") using a loop that repeats until the
* user guesses the correct number.
*
* INPUT
* guess : Integer value guessed by the user
*
* OUTPUT
* feedback : Hints guiding the user until the guess is correct
*
**************************************************************/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
// Seed the random number generator
srand(static_cast<unsigned int>(time(0)));
// Generate a random number (e.g., between 1 and 10)
const int SECRET_NUMBER = (rand() % 10) + 1;
// INPUT
int guess;
cout << "I have generated a random number between 1 and 10.\n";
cout << "Can you guess what it is?\n\n";
// Loop until the user correctly guesses the random number
do {
cout << "Enter your guess: ";
cin >> guess;
if (guess > SECRET_NUMBER) {
cout << "Too high, try again.\n";
} else if (guess < SECRET_NUMBER) {
cout << "Too low, try again.\n";
} else {
cout << "\nCongratulations! You guessed the correct number: " << SECRET_NUMBER << endl;
}
} while (guess != SECRET_NUMBER);
return 0;
}