-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-guesser.cpp
47 lines (40 loc) · 1.06 KB
/
number-guesser.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
#include <iostream>
#include <cmath>
using namespace std;
void guessGame();
//function to play the guesser
void guesser(float high,float low)
{
char answer = 'x', again ='x';
float guess = (low+high)/2;
//guess = round(guess);
cout<< "Is it " << floor(guess) <<"? (h/l/c): ";
cin >> answer;
switch(answer){ //switch block to control high,low,correct
case 'h':
low = round(guess);
break;
case 'l':
high = round(guess);
break;
case 'c':
cout <<"Great! Do you want to play again? (y/n): ";
cin >> again;
if (again == 'y'){
guessGame();
}else{
exit(0);
}
}
guesser(high,low); //if we exit the switch then run the program with updated numbers
}
void guessGame() // initialize the game
{
float high=100,low=1;
cout << "Guess a number between 1 and 100."<< endl;
guesser(high,low);
}
int main()
{
guessGame(); // put it all together
}