-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberGuessingGame.cpp
33 lines (27 loc) · 969 Bytes
/
NumberGuessingGame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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(nullptr)));
// Generate a random number between 1 and 100
int secretNumber =rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "Welcome to the Number Guessing Game!" <<endl;
cout << "I have selected a random number between 1 and 100. Try to guess it." <<endl;
do {
cout << "Enter your guess: ";
cin >> guess;
attempts++;
if (guess < secretNumber) {
cout << "Too low! Try again." <<endl;
} else if (guess > secretNumber) {
cout << "Too high! Try again." <<endl;
} else {
cout << "Congratulations! You guessed the number " << secretNumber << " correctly in " << attempts << " attempts."<<endl;
}
} while (guess != secretNumber);
return 0;
}