forked from DhanushNehru/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_guess_game.cpp
36 lines (30 loc) · 1.16 KB
/
number_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>
int main()
{
std::srand(std::time(0)); // Seed the random number generator
int secretNumber = std::rand() % 100 + 1; // Generate a random number between 1 and 100
int guess;
int attempts = 0;
const int maxAttempts = 3; // You can change this to set the difficulty level
std::cout << "Welcome to the Number Guessing Game!" << std::endl;
std::cout << "I have selected a number between 1 and 100. Can you guess it?" << std::endl;
while (attempts < maxAttempts) {
std::cout << "Enter your guess: ";
std::cin >> guess;
attempts++;
if (guess == secretNumber) {
std::cout << "Congratulations! You guessed the number in " << attempts << " attempts." << std::endl;
break;
} else if (guess < secretNumber) {
std::cout << "Too low! Try again." << std::endl;
} else {
std::cout << "Too high! Try again." << std::endl;
}
}
if (attempts == maxAttempts) {
std::cout << "Sorry, you've used all your attempts. The number was " << secretNumber << "." << std::endl;
}
return 0;
}