-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2numbergusseinggame.cpp
More file actions
59 lines (41 loc) · 1.74 KB
/
2numbergusseinggame.cpp
File metadata and controls
59 lines (41 loc) · 1.74 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
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(static_cast<unsigned int>(time(0)));
char playAgain;
do {
int numberToGuess = rand() % 100 + 1;
int userGuess = 0;
int attempts = 0;
cout << "**** Welcome to the Number Guessing Game! ****" << endl;
cout << "I've picked a secret number between 1 and 100." << endl;
cout << "Think you can guess it? Let's find out!" << endl;
while (userGuess != numberToGuess) {
cout << "\nEnter your guess: ";
cin >> userGuess;
attempts++;
if (userGuess < numberToGuess) {
cout << "Oops! Too low. Aim a bit higher!" << endl;
} else if (abs(userGuess - numberToGuess) <= 10) {
cout << "You're close! Try again.\n";
}
else if (userGuess > numberToGuess) {
cout << "Whoa! That's too high. Try a smaller number." << endl;
} else {
cout << "Hooray! You've guessed the number " << numberToGuess << " correctly!" << endl;
cout << "It took you " << attempts << " attempts. Well done!" << endl;
}
if (userGuess != numberToGuess) {
if (attempts % 5 == 0) {
cout << "Don't give up! You're getting closer." << endl;
}
}
}
cout << "\nWould you like to play again? (y/n): ";
cin >> playAgain;
} while (playAgain == 'y' || playAgain == 'Y');
cout << "\nThanks for playing! Hope to see you again soon!" << endl;
return 0;
}