For my assignment, I need to make a program that guesses the user's number, between 1-19 inclusively, within 5 tries. For each try, the user inputs if the number is: a) correct, b) too high, or c) too low
We are supposed to define two functions:
The first is a function that takes a number ( int
) as a parameter and outputs the prompt to the user that guesses that number (tells the user "Is this your number: <guess>"
) and shows them a menu that explains how to enter correct, high, or low.
The second function is supposed to calculate the next guess after being told if it is too high or too low.
I was able to accomplish this with nested switch
statements, but I am having trouble trying to come up with the second function.
Any help is appreciated. I will try to attach my first program with the switch
statements. I assume I need to generate a random number with the min and max, but I'm not sure how to do it.
#include<iostream>
using namespace std;
int main()
{
int guess = 10;
int input = 0;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch(input) {
case(1):
cout<< "Thanks for playing";
break;
case(2):
guess = 5;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch(input){
case(1):
cout<< "Thanks for playing";
break;
case(2):
guess = 3;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch (input){
case(1):
cout<< "Thanks for playing";
break;
case(2):
guess = 2;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch(input){
case(1):
cout<< "Thanks for playing";
break;
case (2):
guess = 1;
cout<< "Your guess was: "<<guess<<endl;
break;
case (3):
cout<< "Cheater..."<<endl;
break;
}
break;
case(3):
guess = 4;
cout<< "Your guess was: "<<guess<<endl;
break;
}
break;
case(3):
guess = 7;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch (input){
case (1):
cout<< "Thanks for playing";
break;
case(2):
guess = 6;
cout<< "Your guess was: "<<guess<<endl;
break;
case(3):
guess = 8;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch (input){
case (1):
cout<< "Thanks for playing";
break;
case(2):
cout<<"Cheater..."<<endl;
case(3):
guess = 9;
cout<< "Your guess was: "<< guess<<endl;
break;
}
}
break;
}
break;
case(3):
guess = 15;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch(input){
case(1):
cout<< "Thanks for playing";
break;
case(2):
guess = 13;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch (input){
case(1):
cout<< "Thanks for playing";
break;
case(2):
guess = 12;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch(input){
case(1):
cout<< "Thanks for playing";
break;
case (2):
guess = 11;
cout<< "Your guess was: "<<guess<<endl;
break;
case (3):
cout<< "Cheater..."<<endl;
break;
}
break;
case(3):
guess = 14;
cout<< "Your guess was: "<<guess<<endl;
break;
}
break;
case(3):
guess = 17;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch (input){
case (1):
cout<< "Thanks for playing";
break;
case(2):
guess = 16;
cout<< "Your guess was: "<<guess<<endl;
break;
case(3):
guess = 18;
cout<<"Is this your number: "<< guess<<endl;
cout<< "Correct? (1), High?(2), Low(3)"<<endl;
cin>> input;
switch (input){
case (1):
cout<< "Thanks for playing";
break;
case(2):
cout<<"Cheater..."<<endl;
case(3):
guess = 19;
cout<< "Your guess was: "<< guess<<endl;
break;
}
}
break;
}
break;
}
return 0;
}
Here are some observations:
So, given the above characteristics, the Binary Search algorithm would provide an optimal solution ie:
O(log n)
You can do some research on the Binary Search algorithm to get familiar with it. The rest would be your logic of maintaining high
, low
and mid
points. And, you don't need random numbers for this!
You would be using your own variation of the Binary search algorithm that guesses the number by adjusting the problem set ie range where the number may exist.
As far as your two functions are concerned:
For the rest of the boilerplate logic, you can implement that in your main()
function.
Here's a general breakdown of your code (just a synopsis, assuming you're using C++98 or C++03):
int showMenuAndInputNumber() { /* ... */ }
int guessNumber(const int n) { /* ... */ }
int main()
{
const int n = showMenuAndInput();
// validate if n is in the range [1, 19]
// given the search sorted range and the valid number,
// then using Binary search you'll need max 5 tries
// so, you don't need to keep track of the tries
// but, for simplicity, you can use tries
const int tries = 5;
for ( int t = 0; t < tries; ++t )
{
// guess the number here
// adjust the range either right or left
// according to your own Binary search algorithm's variation
}
return 0;
}
This is not meant to be a ready-made solution to your problem. This is just to provide some guidelines.
Hope this helps!
Try this:
#include <iostream>
using namespace std;
#define elif else if
void guessf(int guess) {
cout << "Is this your number: " << guess << endl << "Correct? (1), High?(2), Low(3)" << endl;
}
int main(){
unsigned int range = 10, guess = 10, input = 0, i = 0;
while (i < 5 && input != 1) {
guessf(guess);
cin >> input;
if (range > 1)
range /= 2;
else
range = 1;
if (input == 1)
cout << "Thanks for playing";
elif (input == 2)
guess -= range;
else
guess += range;
}
}
What this does : the guessf()
is pretty simple so we move on to the main()
,the range
variable is what it will be added or subtracted from the guess
on each try, then, until your program does 5 tries or the guess
is correct, ask user if it is correct, high or low, if it is correct cout << "Thanks for playing";
, if the guess
is high subtract the range
and if the guess
is low add the range
to the guess
. Also bcs we deal with int
, the minimum value of range
must be 1
, bcs if it is lower nothing will ever be added or subtracted from the guess
. This technique is at least something similar to "Binary Search"
If you want to remove elif
, remove #define elif else if
and replace elif
with else if
( else
and if
in the same line)
Also by 2 functions, you mean 2 including the main or without? This is an important thing to know
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.