-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
39 lines (30 loc) 路 1.22 KB
/
Copy pathmain.cpp
File metadata and controls
39 lines (30 loc) 路 1.22 KB
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
37
38
39
#include <iostream> // for input and output
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
using namespace std;
int main() {
srand(time(0)); // Seed the random number generator with current time
// Generate a random number between 1 and 100
int numberToGuess = rand() % 100 + 1;
int userGuess; // Variable to store the user's guess
int attempts = 0; // Count the number of attempts
// Introductory message
cout << "Welcome to the Number Guessing Game!\n";
cout << "I'm thinking of a number between 1 and 100.\n";
// Start guessing loop
do {
cout << "Enter your guess: ";
cin >> userGuess; // Read user input
attempts++; // Increment attempt count
// Give feedback based on the guess
if (userGuess > numberToGuess) {
cout << "Too high! Try again.\n";
} else if (userGuess < numberToGuess) {
cout << "Too low! Try again.\n";
} else {
// Correct guess
cout << "馃帀 Congratulations! You guessed it in " << attempts << " attempts.\n";
}
} while (userGuess != numberToGuess); // Loop until the correct guess
return 0; // Exit the program
}