#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
// Seed the random number generator with the current time
srand(static_cast<unsigned int>(time(0)));
// Generate a random number between 1 and 100
int randomNumber = rand() % 100 + 1;
int userGuess = 0;
int attempts = 0;
const int maxAttempts = 5;
cout << "Welcome to the Number Guessing Game!" << endl;
cout << "I have generated a random number between 1 and 100." << endl;
cout << "You have " << maxAttempts << " attempts to guess the number!" << endl;
// Loop until the user guesses the number or exceeds the max attempts
while (userGuess != randomNumber && attempts < maxAttempts) {
cout << "Enter your guess: ";
cin >> userGuess;
attempts++;
if (userGuess < randomNumber) {
cout << "Too low! Try again." << endl;
} else if (userGuess > randomNumber) {
cout << "Too high! Try again." << endl;
} else {
cout << "Congratulations! You guessed the number!" << endl;
return 0; // Exit the program if guessed correctly
}
}
// If the user has used all attempts, reveal the number
cout << "Sorry! You've used all your attempts. The number was " << randomNumber
<< "." << endl;
return 0;
}