-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMC_0515.cpp
70 lines (57 loc) · 1.52 KB
/
HMC_0515.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Chapter 5: Programming Exercise # 15
* The program in Example 5-6 implements the Number Guessing Game.
* However, in that program, the user is given as many tries as needed to guess the correct number.
* Rewrite the program so that the user has no more than 5 tries to guess the correct number.
* Your program should print an appropriate message, such as "You win!" or "You lose!".
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char ch1;
int num, guess, count=1;
bool isGuessed;
srand(time(0));
num = rand() % 100;
isGuessed = false;
while (!isGuessed)
{
cout << "Would you like to begin the game? [Y/N]: ";
cin >> ch1;
cout << endl;
if (ch1 != 'Y')
{
isGuessed = true;
}
else
isGuessed = false;
while ((count <= 5) && (ch1 == 'Y'))
{
cout << "Enter an integer greater than or equal to 0 & less than 100: ";
cin >> guess;
cout << endl;
if (guess == num)
{
cout << "You guessed the correct number. You Won!" << endl;
isGuessed = true;
}
else if (guess < num && count < 5)
cout << "Your guess is lower than the number. \n Guess again!" << endl;
else if (guess > num && count < 5)
cout << "Your guess is higher than the number. \n Guess again!" << endl;
count++;
}
cout << "Sorry, You Lost :<";
cout << endl << "Play Again? [Y/N]" << endl;
cin >> ch1;
if (ch1 != 'Y')
{
isGuessed = true;
}
else
isGuessed = false;
}
return 0;
}