-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_game.cpp
36 lines (27 loc) · 954 Bytes
/
guess_game.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
34
35
36
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL)); // initialize random seed
int number = rand() % 100 + 1; // generate random number between 1 and 100
int guess; // initialize variable for user's guess
int attempts = 0; // initialize variable for number of attempts
cout << "Welcome to the number guessing game!" << endl;
while (guess != number) {
cout << "Enter your guess (1-100): ";
cin >> guess;
if (guess < number) {
cout << "Your guess is too low. Try again." << endl;
attempts++;
}
else if (guess > number) {
cout << "Your guess is too high. Try again." << endl;
attempts++;
}
else {
cout << "Congratulations! You guessed the number in " << attempts << " attempts." << endl;
}
}
return 0;
}