Here you go,
#include <iostream>
using namespace std;
int main() {
int secretNumber = 42; // Hardcoded secret number
int guess;
char choice;
do {
cout << "Guess the secret number (1 to 100): ";
cin >> guess;
if (guess == secretNumber) {
cout << "Congratulations! You guessed it right.\n";
} else if (guess < secretNumber) {
cout << "Too low! Try again.\n";
} else {
cout << "Too high! Try again.\n";
}
cout << "Do you want to try again? (y/n): ";
cin >> choice;
switch (choice) {
case 'y':
case 'Y':
// loop continues
break;
case 'n':
case 'N':
cout << "Thanks for playing!\n";
break;
default:
cout << "Invalid input. Exiting.\n";
choice = 'n'; // force exit
break;
}
} while (choice == 'y' || choice == 'Y');
return 0;
}