Feb 5, 2017 at 10:02am Feb 5, 2017 at 10:02am UTC
I got really confuse on putting in arguments for the getUserResponseToGuess() function. I don't really know what to put into that argument. I wanted the user to think of a number in their mind, and then let the problem to guess it. But everytime I try to run my program. It didn't works out as I expected. Please help me.
the (h/l/c) means, higher, lower, and correct.
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
#include <iostream>
using namespace std;
int getMidpoint(int low, int high) {
int mid = (low + high) / 2;
return mid;
}
void getUserResponseToGuess(int guess, char & result) {
cout << "Is it " << guess << " (h/l/c) ? " ;
if (result == 'h' ) {
cout << "Is it " << getMidpoint(guess, 100) << " (h/l/c) ? " ;
}else if (result == 'l' ) {
cout << "Is it " << getMidpoint(1, guess) << " (h/l/c) ? " ;
}
}
}
void playOneGame() {
cout << "Think of a number between 1 and 100." << endl;
getUserResponseToGuess();
}
int main() {
char response;
cout << "Ready to play (y/n)? " ;
cin >> response;
while (response == 'y' ) {
playOneGame();
cout << "Great! Do you want to play again (y/n)? " ;
cin >> response;
}
}
This is the other version I tried again. But still not working as I expected. Please help.
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>
using namespace std;
int getMidpoint(int low, int high) {
int mid = (low + high) / 2;
return mid;
}
void getUserResponseToGuess(int guess, char & result) {
while (result != 'c' ) {
if (result = 'h' ) {
getMidpoint(guess, 100);
}
else if (result = 'l' ) {
getMidpoint(1, guess);
}
}
}
void playOneGame() {
char userInput;
cout << "Think of a number between 1 and 100." << endl;
cout << "Is it " << getMidpoint(1, 100) << " (h/l/c) ? " << endl;
cin >> userInput;
getUserResponseToGuess(getMidpoint(1, 100), userInput);
}
int main() {
char response;
cout << "Ready to play (y/n)? " ;
cin >> response;
while (response == 'y' ) {
playOneGame();
cout << "Great! Do you want to play again (y/n)? " ;
cin >> response;
}
}
Last edited on Feb 5, 2017 at 10:22am Feb 5, 2017 at 10:22am UTC
Feb 5, 2017 at 10:53am Feb 5, 2017 at 10:53am UTC
I am not sure, but maybe you should be thinking about random numbers because you said you want "the user to think of a number in mind".
Last edited on Feb 5, 2017 at 10:55am Feb 5, 2017 at 10:55am UTC