Recursion function won't exit

This is a discussion on Recursion function won't exit within the C++ Programming forums, part of the General Programming Boards category; I know that you should quit when something works but you don't learn much that way. I substituted all the ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    26
    I know that you should quit when something works but you don't learn much that way. I substituted all the cin statements in the recursion with variable = cin.get()

    The program then did the following:
    If num was >1 then the screen scrolled forever after the string was entered. If num = 1, then the first example did'nt output the letter but every example worked after that.

    What is the difference between cin and cin.get?

    What is not being initialized or being initialized with the cin.get?

    Code follows:
    #include <iostream>


    using std::cout;
    using std::cin;
    using std::endl;
    using std::boolalpha;

    bool palindrome (int, int);

    int main()
    {
    const int StopVal = 0;
    int num;
    cout << "Enter the number of letters in your candidate palindrome, or\n"
    << "enter " << StopVal << " to end this program.\n\n";
    cout << "Enter the number of letters (" << StopVal << " to stop) here ==> ";
    cin >> num;

    while (num != StopVal) {
    cout << "Enter the palindrome candidate ==> ";
    if(palindrome(num/2, num%2))
    cout << " is a palindrome.\n" << endl;
    else
    cout << " is not a palindrome.\n" << endl;
    cout << "Enter number of letters for the next candidate ("
    << StopVal << " to stop) ==> ";
    cin >> num;
    cin.get();
    }
    return 0;
    }

    bool palindrome ( int half , int mod )
    {
    char letter1, letter2, oddletter;
    static bool status = true;
    static bool oddgot = false;

    if ( half == 0 ) { // Handles case with only one character
    letter1 = cin.get();
    cout << letter1;
    status = true;
    return status;
    }

    if ( half-- >= 1){
    status=true;
    oddgot=false;
    letter1 = cin.get();
    cout << letter1;
    if (half>0)
    palindrome ( half, mod );

    if ( mod == 1 && oddgot==false) {
    oddletter = cin.get();
    cout << oddletter;
    oddgot=true;
    }

    letter2 = cin.get();
    cout << letter2;
    if ( letter1 != letter2 )
    status = false;



    }
    return status;
    }

  2. #17
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    cin.get() will take everything that's inputted - whitespace and newlines, so you'll have to take these into account for each input. As the >> operator will do the job I don't see why you'd want to make it more complicated.
    zen

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    26
    My book says that letter1 = cin.get() will only read the next character and puts that in letter1.

    Can something be put in the () to limit the input to 1 character?

  4. #19
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Yes, it'll only read one character, but that character can be ' ' or '\n' etc. You could probably use an overloaded version of get() to ensure that these characters aren't picked up, but then you'd be copying the way that the >> operator works, so what would be the point?
    zen

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    26
    What do you mean by overloaded version of get()? I would appreciate an example.

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    26

    Thumbs up

    A liitle work on my own solved the questions.

    I changed all the cin >> to variable = cin.get();

    I then added one line to the main() . Since the problem occured at initialization, I found that a cin.get(); placed just before the while ( ) statement emptied the buffer until the first character.

    This is why I wanted to check the difference between the cin>> and the variable = cin.get(). It is a subtle difference that might help me solve a problem in the future.

    Many thanks for your help.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21