#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void playGame(int lower, int upper) {
srand(time(0));
int numberToGuess = lower + rand() % (upper - lower + 1);
int guess, attempts = 0;
cout << "Guess the number between " << lower << " and " << upper << "!" << endl;
do {
cout << "Enter your guess: ";
cin >> guess;
attempts++;
if (guess > numberToGuess) {
cout << guess << " is too high! Try again." << endl;
} else if (guess < numberToGuess) {
cout << guess << " is too low! Try again." << endl;
} else {
cout << "Congratulations! You guessed the number in " << attempts << " attempts." << endl;
}
} while (guess != numberToGuess);
}
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Welcome, " << name << "! Let's play a number guessing game.\n" << endl;
int lower = 1, upper = 100;
char choice;
do {
playGame(lower, upper);
cout << "Do you want to play again? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
cout << "Thanks for playing, " << name << "!" << endl;
return 0;
}