#include #include #include #include #include using namespace std; int main() { cout << "I am thinking of a number in the range 1 to 100 inclusive.\n" << "Keep trying until you guess it.\n\n"; unsigned seed {chrono::system_clock::now().time_since_epoch().count()}; default_random_engine engine {seed}; uniform_int_distribution distribution {1, 100}; auto r {bind(distribution, engine)}; const int correct {r()}; int guesses {0}; for (;;) { cout << "Go ahead: "; int guess {0}; cin >> guess; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } ++guesses; if (guess == correct) { break; } if (guess < correct) { cout << "Too low."; } else { cout << "Too high."; } cout << " Try again.\n\n"; } cout << "That's right, the number was " << correct << ".\n"; cout << "It only took you " << guesses << " guess" << (guesses == 1 ? "" : "es") << "!\n"; return EXIT_SUCCESS; }