-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_guessing_game.cpp
More file actions
40 lines (32 loc) · 1.15 KB
/
Copy pathnumber_guessing_game.cpp
File metadata and controls
40 lines (32 loc) · 1.15 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
40
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0)); // Seed the random number generator
int secretNumber = rand() % 100 + 1; // Generate a random number between 1 and 100
int guess;
int attempts = 0;
bool guessedCorrectly = false;
cout << "Welcome to the Number Guessing Game!" << endl;
cout << "I'm thinking of a number between 1 and 100. Try to guess it." << endl;
while (!guessedCorrectly) {
cout << "Enter your guess: ";
cin >> guess;
attempts++;
if (cin.fail()) { // Handle non-numeric input
cin.clear();
cout << "Invalid input. Please enter a number." << endl;
continue; // Skip the rest of the loop and ask for input again
}
if (guess < secretNumber) {
cout << "Too low! Try again." << endl;
} else if (guess > secretNumber) {
cout << "Too high! Try again." << endl;
} else {
cout << "Congratulations! You guessed the number in " << attempts << " attempts." << endl;
guessedCorrectly = true;
}
}
return 0;
}