A simple game where the user guesses a number between 1 and 100.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(static_cast<unsigned int>(time(0)));
int number = rand() % 100 + 1; // Random number between 1 and 100
int guess;
int attempts = 0;
cout << "Guess the number (between 1 and 100): ";
do {
cin >> guess;
attempts++;
if (guess > number) {
cout << "Too high! Try again: ";
} else if (guess < number) {
cout << "Too low! Try again: ";
} else {
cout << "Congratulations! You guessed it in " << attempts << " attempts.\n";
}
} while (guess != number);
return 0;
}
Leave a Reply