I am writing this program that will guess what the user number is thinking about. After several days of work, I could not understand what was wrong with that. Also my proposed class for assignment is not what I expected. Please, help.
- A user can guess 100, but my program uses a midpoint rule, so it can reach 99. How can I make 100 inclusive?
- If I keep pressing "l", the program will eventually exit the loop and print. If you want to try again?
- Is there a better way to code this program? An example, please.
Here is the real program:
Write a program in which you can find the number chosen by the person. The human user will think of a quantity from 1 to 100. The program will make guesses, and the user will tell the program to guess higher or lower. The program should find the middle of the two numbers and ask if this number is larger or lower.
#include <iostream>
using namespace std;
int main() {
char check;
char tryagain;
do {
int midpoint;
const int MAX = 100;
const int MIN = 1;
int y = MAX;
int x = MIN;
cout << "Think of a number between 1 and 100." << endl;
midpoint = (x + y) / 2;
while (x != y || y != x) {
cout << endl << "Is it" << " " << midpoint << " " << "?";
cin >> check;
if (check == 'l' || check == 'L') {
y = midpoint;
midpoint = (x + y) / 2;
}
else if (check == 'h' || check == 'H') {
x = midpoint;
midpoint = (x + y) / 2;
}
else if (check == 'c' || check == 'C') {
break;
}
else {
cout << "Incorrect choice." << endl;
}
}
cout << "Great! Do you want to try again? (y/n)";
cout << endl;
cin >> tryagain;
} while (tryagain == 'y' || tryagain != 'n');
return 0;
}
source
share