while (true) { int target = dist(gen); int guess = 0; int attempts = 0;
std::cout << "\n========== Guess the Number ==========\n"; std::cout << "A number between 1 and 100 has been generated.\n"; std::cout << "Enter 0 to quit.\n\n";
if (!(std::cin >> guess)) { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "Please enter a valid number!\n"; continue; }
if (guess == 0) { std::cout << "Exited the game.\n"; return0; }
attempts++;
if (guess < target) { std::cout << "Too low, try again!\n"; } elseif (guess > target) { std::cout << "Too high, try again!\n"; } else { std::cout << "\nCongratulations! The number was " << target << "!\n"; std::cout << "You got it in " << attempts << " attempt(s).\n"; break; } }
std::cout << "\nPress 1 to play again, any other key to exit: "; int play_again; if (!(std::cin >> play_again) || play_again != 1) { std::cout << "Thanks for playing, goodbye!\n"; break; } }