-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessTheNumber.cpp
More file actions
45 lines (43 loc) · 1.38 KB
/
guessTheNumber.cpp
File metadata and controls
45 lines (43 loc) · 1.38 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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
int number = rand() % 101;
cout << "Wellcome to the guess the number game \nComputer has taken a number between 1 to 100 and you have to guess it \nNow guess the number " << endl;
int in, a = 0;
do
{
cin >> in;
if (in > number + 20)
{
cout << "You are too far. Guess something smaller" << endl;
}
else if (in < number - 20)
{
cout << "You are too far. Guess something bigger" << endl;
}
else if (in > number + 10 && in <= number + 20)
{
cout << "You are close. Guess something smaller" << endl;
}
else if (in < number - 10 && in >= number - 20)
{
cout << "You are close. Guess something bigger" << endl;
}
else if (in > number && in <= number + 10)
{
cout << "You are so close. Guess something smaller" << endl;
}
else if (in < number && in >= number - 10)
{
cout << "You are so close. Guess something bigger" << endl;
}
a++;
}while(in != number);
cout << "Congratulations, You have guessed the number.\nIt was " << number << endl;
cout << "You have done it in " << a << " attempts" << endl;
return 0;
}