-
Notifications
You must be signed in to change notification settings - Fork 0
/
num-guess.cpp
36 lines (28 loc) · 1009 Bytes
/
num-guess.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
#include <iostream>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
using namespace std;
int main() {
// Initialize random seed based on current time
srand(static_cast<unsigned int>(time(0)));
int randomNumber = rand() % 100 + 1; // Random number between 1 and 100
int guess = 0;
int attempts = 0;
cout << "Welcome to the Number Guessing Game!" << endl;
cout << "I'm thinking of a number between 1 and 100. Can you guess what it is?"; // Fixed line
cout << endl;
// Game loop
while (guess != randomNumber) {
cout << "Enter your guess: ";
cin >> guess;
attempts++;
if (guess < randomNumber) {
cout << "Too low! Try again." << endl;
} else if (guess > randomNumber) {
cout << "Too high! Try again." << endl;
} else {
cout << "Congratulations! You've guessed the number in " << attempts << " attempts." << endl;
}
}
return 0;
}