-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathguessing_game1.cpp
34 lines (27 loc) · 964 Bytes
/
guessing_game1.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
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
// Seed the random number generator
std::srand(static_cast<unsigned>(std::time(nullptr)));
// Generate a random number between 1 and 100
int targetNumber = std::rand() % 100 + 1;
int playerGuess;
int attempts = 0;
std::cout << "Welcome to the Number Guessing Game!\n";
std::cout << "I've selected a random number between 1 and 100.\n";
while (true) {
std::cout << "Your guess: ";
std::cin >> playerGuess;
attempts++;
if (playerGuess < targetNumber) {
std::cout << "Too low!\n";
} else if (playerGuess > targetNumber) {
std::cout << "Too high!\n";
} else {
std::cout << "Congratulations! You guessed the correct number (" << targetNumber << ") in " << attempts << " attempts.\n";
break;
}
}
return 0;
}