# Thread: Recursive Function (Guessing Game)

1. ## Recursive Function (Guessing Game)

Hi:

I am practicing using a recursive function. I created a guess my number game. The program randomly generates a number in main() and then passes this into the recurse function. I call the recurse function from main.

1) The error message is placed in the code.

2) I declared the user variable input number inside of the recurse function instead of passing this in. I tried to pass in but then there was an error message that there were too many arguements.

Code:
```void recurse (int iGuessIn)
{
//Number input by user.
int iNumberIn = 0;
cin>>iNumberIn;
system("pause");

if (iGuessIn < iNumberIn) {
//Error message here reporting "error primary-expression before '<<' token."
cout<<"Sorry. Wrong guess.  Try again. "; <<endl;
cin>>iNumberIn;
system("pause");
recurse (iGuessIn, iNumberIn);
}//if
else if (iGuessIn > iNumberIn) {
cout<<"Sorry. Wrong guess.  Try again. "; <<endl;
recurse (iGuessIn, iNumberIn);
}//else if
else (iGuessIn == iNumberIn) {
cout<<"Correct!  My number was "; <<iGuessIn; <<endl;
return;
}//else
}//recurse```

2. Originally Posted by JKid314159
Code:
```void recurse (int iGuessIn)
{
//Number input by user.
int iNumberIn = 0;
cin>>iNumberIn;
system("pause");

if (iGuessIn < iNumberIn) {
//Error message here reporting "error primary-expression before '<<' token."
cout<<"Sorry. Wrong guess.  Try again. "; <<endl;
cin>>iNumberIn;
system("pause");
recurse (iGuessIn, iNumberIn);
}//if
else if (iGuessIn > iNumberIn) {
cout<<"Sorry. Wrong guess.  Try again. "; <<endl;
recurse (iGuessIn, iNumberIn);
}//else if
else (iGuessIn == iNumberIn) {
cout<<"Correct!  My number was "; <<iGuessIn; <<endl;
return;
}//else
}//recurse```
Take a look at those parts, do you see anything wrong? Of course there's an error for too many arguments, you only defined one argument in the function definition yet you're attempting to pass two.

3. Sorry. This was not the up to date code. Hopefully this is it. Thank you.
The call from main calls recurse fn w/parameter of iGuessIn. Once in, the users input variable is initialized and then the input is received by cin>>. Then the conditionals statements are traversed. So, if the number is too low, the first if statement is executed with cout. This then calls recurse again. This process occurs until the guess is correct. But once return, are the calls to previous recurse in a thread of queues?
Code:
```void recurse (int iGuessIn)
{
//Number input by user.
int iNumberIn = 0;
cin>>iNumberIn;

if (iGuessIn < iNumberIn) {
cout<<"Sorry. Guess too low.  Try again. "; <<endl;
recurse (iGuessIn);
}//if
else if (iGuessIn > iNumberIn) {
cout<<"Sorry. Guess too high.  Try again. "; <<endl;
recurse (iGuessIn);
}//else if
else (iGuessIn == iNumberIn) {
cout<<"Correct!  My number was "; <<iGuessIn; <<endl;
return;
}//else
}//recurse

int main()

{
//Number randomly generated by program.
const int iGuess = rand() * 1000 + 1;
//Console out/in.
cout<<"Can you guess the number I am thinking about."; <<endl;
//Pass program generated number and user input to recursive function.
recurse (iGuess);

char f;
cin >> f;

return 0;

}//main```

4. cin / cout = C++, so you probably want the other forum.
Code:
```void foo( int secretnumber )
{
int guess = getnumberfromuser();
if( secretnumber == guess )
{
output( "Yay!" );
return;
}
if( secretnumber < guess )
output( "Guess higher." );
else
output( "Guess lower." );
foo( secretnumber );
}```

Quzah.

5. I think I found out what it is? The last else statement should not have a conditional statement. Also, the code is C++ and I just realized this is C forum. Best.