-
Notifications
You must be signed in to change notification settings - Fork 0
/
Num_Guessor.cpp
40 lines (35 loc) · 994 Bytes
/
Num_Guessor.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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
int tries = 0;
int guess;
srand(time(0));
int num = rand() % 5 +1;
const int TRIES_LIMIT = 3;
while(tries < TRIES_LIMIT){
cout << "Welcome to random number guessor\n";
cout << "Enter a number between 1-5\n";
cin >> guess;
tries++;
while(cin.fail()){
cin.clear();
string dummy;
cin >> dummy;
cout << "Invalid input. Use a number between 1-5\n";
cin >> num;
}
if(guess > num){
cout << "Too High. Tries taken: " << tries << '\n';
}else if(guess < num){
cout << "Too Low. Tries taken: " << tries << '\n';
}else{
cout << "You got it right. Tries taken: " << tries << '\n';
break;
}
}
if(tries == TRIES_LIMIT){
cout << "You have exhausted all your tries.\n";
}
}