Thread: Recursive Function (Guessing Game)

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    New York City
    Posts
    3

    Unhappy 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. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by JKid314159 View Post
    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. #3
    Registered User
    Join Date
    Nov 2009
    Location
    New York City
    Posts
    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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Location
    New York City
    Posts
    3

    Wink

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM