Why are my numbers not calculating correctly? My teacher wouldn't give me more help because he thought it was close enough to his output. But I'm not satisfied. First of all here is my finished code:
#include <iostream>
using namespace std;
void playOneGame();
char getUserResponseToGuess(int);
int getMidpoint(int, int);
bool shouldPlayAgain();
int main()
{
do
{
playOneGame();
} while (shouldPlayAgain());
return 0;
}
void playOneGame()
{
int min = 0;
int max = 100;
int num;
char answer;
cout << "Think of a number between 1 and 100." << endl;
num = getMidpoint(min, max);
answer = getUserResponseToGuess(num);
while(answer != 'c')
{
if(answer == 'l')
{
max = num;
max--;
num = getMidpoint(min, max);
answer = getUserResponseToGuess(num);
}
else if(answer == 'h')
{
min = num;
min++;
num = getMidpoint(min, max);
answer = getUserResponseToGuess(num);
}
}
}
bool shouldPlayAgain()
{
char playAgain;
cout << "Great! Do you want to play again? (y/n): ";
cin >> playAgain;
if(playAgain == 'y')
{
return true;
}
else
{
return false;
}
}
char getUserResponseToGuess(int guess)
{
char input;
cout << "Is it " << guess << "? (h/l/c):";
cin >> input;
return input;
}
int getMidpoint(int low, int high)
{
double midP = (low + high) / 2;
return midP;
}
This is the expected output:
Think of a number between 1 and 100
Is the number 50? (y/n/c): h
Is the number 75? (y/n/c): l
Is the number 62? (y/n/c): h
Is the number 69? (y/n/c): h
Is the number 72? (y/n/c):
This is the output MY program gets:
Think of a number between 1 and 100
Is the number 50? (y/n/c): h
Is the number 75? (y/n/c): l
Is the number 62? (y/n/c): h
Is the number 68? (y/n/c): h
Is the number 71? (y/n/c):
I just want to know why my numbers aren't calculating correctly. As you can see it is not by a huge difference but it still bothers me! I also can't change the int parameters, they must be ints!