-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.cpp
More file actions
47 lines (37 loc) · 1.33 KB
/
tempCodeRunnerFile.cpp
File metadata and controls
47 lines (37 loc) · 1.33 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
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
using namespace std;
int main() {
// Step 1: Seed the random number generator
srand(time(0)); // ensures different random number each time
// Step 2: Generate a random number between 1 and 10
int secretNumber = rand() % 10 + 1;
int guess;
bool isGuessed = false;
cout << "🎯 Welcome to Guess the Number Game!" << endl;
cout << "I have chosen a number between 1 and 10." << endl;
cout << "You have 3 attempts to guess it!" << endl;
// Step 3: Give user 3 attempts
for (int attempt = 1; attempt <= 3; attempt++) {
cout << "\nAttempt " << attempt << ": Enter your guess: ";
cin >> guess;
if (guess == secretNumber) {
cout << "🎉 Congratulations! You guessed it right!" << endl;
isGuessed = true;
break;
}
else if (guess < secretNumber) {
cout << "Too low! Try a higher number." << endl;
}
else {
cout << "Too high! Try a lower number." << endl;
}
}
// Step 4: Reveal answer if not guessed
if (!isGuessed) {
cout << "\n❌ Sorry! The correct number was " << secretNumber << "." << endl;
}
cout << "\nThanks for playing! 😊" << endl;
return 0;
}