im trying to write a recursive function that will never ask user for a number but the program will guess by asking if the number is greater then a number (the user says y/n) then the computer asks again .... i dont really understand recursive function at all and am having a terrible time with this any help pointing me in the right direction would be greatly appreciated
You should study recursion in depth first. As you know, if you don't understand that you won't be able to write the program. I assume this is an assignment of some sort; you should probably consult your textbook on the topic.
#include <iostream>
#include <math.h>
#include <stdlib.h>
usingnamespace std;
bool isYourNumber(int number)
{
char response;
do
{
// Ask the user if their number is the given number
cout << "Is the number " << number << "? (y/n) ";
// Get their response
cin >> response;
}
while (response != 'y' && response != 'Y' &&
response != 'n' && response != 'N');
return ((response == 'y') || (response == 'Y'));
}
bool isYourNumberGreaterThan(int number)
{
char response;
do
{
// Ask the user if their number is the given number
cout << "Is the number greater than " << number << "? (y/n) ";
// Get their response
cin >> response;
}
while (response != 'y' && response != 'Y' &&
response != 'n' && response != 'N');
return ((response == 'y') || (response == 'Y'));
}
int askFor(srand %101)
{
char yes= ('y' || 'Y');
char no= ('n' || 'N');
int number = (start+end)/2;
cout << " is the number " << number << "?" << endl;
if (yes=='y' || yes== 'Y')
{
cout << "i'm awesome, i knew i could get it right!" << endl;
return number;
}
else
{
cout << "is the number greater than " << number << "?" << endl;
if (yes== 'y' || yes== 'Y')
{
return askFor(number, end);
}
elsereturn askFor(start,number);
}
}
int main()
{
askFor;
return 0;
}
Um I don't think this even compiles. int askfor(srand%101) {
Read up on functions. You try to call this function recursively with two parameters, but the function's parameter list is screwed up.
1 2
char yes= ('y' || 'Y');
char no= ('n' || 'N');
This compiles but doesn't do anything useful. I'm not even sure what you are trying to do here.
askFor;
You're trying to call a function here that (as above) is probably intended to take two parameters...but here you don't even use parentheses to call it.
The compiler is not going to look at function names and guess parameters or code at all, though it looks like you are expecting it to...