-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumGuess.cpp
More file actions
55 lines (52 loc) · 1.26 KB
/
Copy pathnumGuess.cpp
File metadata and controls
55 lines (52 loc) · 1.26 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
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
// taking user input to generate random value
cout << "Enter any value to generate a random number: ";
int seed_value;
cin >> seed_value;
srand(seed_value);
// generating random value and storing
int random = rand();
// cout << random;
cout << "Random value generated! Start guessing it\nEnter number: ";
double num;
int count = 0;
while (true)
{
cin >> num;
if (num == random)
{
cout << "Congratulations! You guessed it right";
break;
}
if (num > random)
{
cout << num << " is high. Enter again: ";
}
if (num < random)
{
cout << num << " is less, Enter again: ";
}
count++;
}
// prompt if user want to see how many times it take to guess
cout << "\nDo you want to display, how many times it took to guess? 'yes'(y),'no'(n): ";
char ch;
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
cout << "It took " << count + 1 << " times to guess it";
}
else if (ch == 'n' || ch == 'N')
{
cout << "OK, Thankyou!";
}
else
{
cout << "Invalid input";
}
return 0;
}