#include // This program has Player A enter a secret integer // and then repeatedly asks Player B to guess that // number. // // Exercises: // // 1. Modify the program so that Player A must enter // an integer from 1 through 100, inclusive. If // Player A enters a value outside this range, // repeatedly query Player A until a valid value // is entered. // // 2. Currently, Player B is only told when the // secret number has been guessed. Modify the // program so that after each guess, exactly one // of the following messages is printed: // // You guessed the secret number. // Your guess is too high. // Your guess is too low. // // 3. Now make the program remember information that // it has already given Player B. // // For example, suppose Player B guesses 17 and // the program responds // // Your guess is too high. // // If Player B later guesses 17 or any larger // number, instead print // // No, I already told you 17 was too high... // // Similarly, suppose Player B guesses 42 and the // program responds // // Your guess is too low. // // If Player B later guesses 42 or any smaller // number, instead print // // No, I already told you 42 was too low... // // You will therefore have to remember the // smallest number that has been reported as too // high and the largest number that has been // reported as too low. // Function declarations int main(); // Function definitions int main() { int secret_number{}; std::cout << "Player A: enter a secret number: "; std::cin >> secret_number; while ( true ) { int guessed_number{}; std::cout << "Player B: enter a guess: "; std::cin >> guessed_number; if ( guessed_number == secret_number ) { std::cout << "You guessed the secret number." << std::endl; break; } } return 0; } // Hint for Exercise 1: // // You have already seen how a loop of the form // // while ( true ) { // // Query the user. // // if ( /* the value is valid */ ) { // break; // } // } // // can be used to repeatedly query the user until // an acceptable value is entered. // Hint for Exercise 2: // // There are three possibilities: // // guessed_number == secret_number // guessed_number > secret_number // guessed_number < secret_number // // An if-else-if-else statement can distinguish // between these three cases. // Hint for Exercise 3: // // At any point in the game, Player B may already // know that the secret number must lie within a // smaller range. // // For example, if 20 was too low and 70 was too // high, then the secret number must satisfy // // 20 < secret_number < 70. // // You could therefore use two local variables to // remember the best information obtained so far: // // - the largest guess known to be too low, and // - the smallest guess known to be too high. // // Since the secret number is between 1 and 100, // what initial values could you give these two // variables? // Here is one solution: /* int main() { int secret_number{}; while ( true ) { std::cout << "Player A: enter a secret integer " << "from 1 through 100: "; std::cin >> secret_number; if ( (secret_number >= 1) && (secret_number <= 100) ) { break; } } int too_low{ 0 }; int too_high{ 101 }; while ( true ) { int guessed_number{}; std::cout << "Player B: enter a guess: "; std::cin >> guessed_number; if ( guessed_number <= too_low ) { std::cout << "No, I already told you " << too_low << " was too low..." << std::endl; } else if ( guessed_number >= too_high ) { std::cout << "No, I already told you " << too_high << " was too high..." << std::endl; } else if ( guessed_number == secret_number ) { std::cout << "You guessed the secret number." << std::endl; break; } else if ( guessed_number < secret_number ) { std::cout << "Your guess is too low." << std::endl; too_low = guessed_number; } else { std::cout << "Your guess is too high." << std::endl; too_high = guessed_number; } } return 0; } */