Results 1 to 8 of 8

Thread: getline() not being executed in program

  1. #1
    Join Date
    Jul 2008
    Location
    Aryadesa
    Beans
    183
    Distro
    Ubuntu 12.04 Precise Pangolin

    getline() not being executed in program

    getline is just being ignored in the code! After first input 'yes' is displayed.
    Code:
    int main()
    {
        int t,i, len=0, k, T;
        string pattern;
        while ( cin >> T ){
            for (i=0; i<T; i++){
                getline( cin, pattern );
                len = pattern.length();
                if ( len == 0 ){
                    cout << "Yes" << endl;
                    continue;
                }
                if ( len%2 != 0 ){
                    cout << "No" << endl;
                    continue;
                }
                if ( valid( pattern, len ) ) cout << "Yes" << endl;
                else cout << "No" << endl;
            }
            pattern.clear();
        }
        return 0;
    }
    Quantum computers are not known to be able to solve NP-complete problems in polynomial time.
    Scott Aaronson's blog

  2. #2
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: getline() not being executed in program

    <never mind, code as posted won't even compile for me.....>
    Last edited by lisati; October 1st, 2009 at 02:20 AM. Reason: Someone else who knows more C++ than me is free to jump in.
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  3. #3
    Join Date
    Jul 2008
    Location
    Aryadesa
    Beans
    183
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: getline() not being executed in program

    Its is already included.
    Quantum computers are not known to be able to solve NP-complete problems in polynomial time.
    Scott Aaronson's blog

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: getline() not being executed in program

    Code:
    ...
        while ( cin >> T ){
            cin.ignore(100, '\n'); // 100 was arbitrarily chosen.
    
            ...
    Read here for more info: http://www.cplusplus.com/reference/i...stream/ignore/
    Last edited by dwhitney67; October 1st, 2009 at 02:20 AM.

  5. #5
    Join Date
    Jul 2008
    Location
    Aryadesa
    Beans
    183
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: getline() not being executed in program

    Quote Originally Posted by dwhitney67 View Post
    Code:
    ...
        while ( cin >> T ){
            cin.ignore(100, '\n'); // 100 was arbitrarily chosen.
    
            ...
    Read here for more info: http://www.cplusplus.com/reference/i...stream/ignore/
    I got that but I dont see how it solves my problem. getline() fucntion is skipped for no reason.

    Thanks
    Quantum computers are not known to be able to solve NP-complete problems in polynomial time.
    Scott Aaronson's blog

  6. #6
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: getline() not being executed in program

    maybe your logic is wrong
    if you input according to what is expected by that code it does not get skipped:
    a number followed by N strings followed by newline e.g.
    3 fgdf
    fg
    fg

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: getline() not being executed in program

    Quote Originally Posted by kcode View Post
    I got that but I dont see how it solves my problem. getline() fucntion is skipped for no reason.

    Thanks
    It is not skipped; seriously, did you step through your code using the debugger? Or at a minimum, place trace print out statements in your code?

    The getline() is being called, and it is cheerfully gobbling up the newline character that you left behind in the input stream (cin) after you prompted the user to enter a numeric value.

    cin will fulfill parsing the int-value that is entered, and will consider the input up to, but not including, the newline that is entered. So even if you enter 123abc<enter>, cin will return 123. The rest of the "crap" is still sitting in the input stream.

    If you still have your doubts, try this test program, and when running it, enter "123abc<enter>" at the prompt.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
       using std::cout;
       using std::cin;
       using std::endl;
    
       int         number;
       std::string crap;
    
       cout << "Enter a number: ";
       cin  >> number;
    
       getline(cin, crap);
    
       cout << "Number entered: " << number << endl;
       cout << "Add't crap    : " << crap   << endl;
    }
    Now with respect to the cin.ignore(), it will discard the "crap" by flushing the input stream up to and including the delimiter character specified. In the example I provided in the earlier post, I specified the '\n' as the delimiter, and I told it to flush up to 100 characters.

    P.S. If when running your app, you do not enter a numeric value when prompted, the while loop will never be traversed because cin will convert its state to a boolean, which will be false.
    Last edited by dwhitney67; October 3rd, 2009 at 01:03 PM.

  8. #8
    Join Date
    Jul 2008
    Location
    Aryadesa
    Beans
    183
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: getline() not being executed in program

    Thanks, got it.
    Quantum computers are not known to be able to solve NP-complete problems in polynomial time.
    Scott Aaronson's blog

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •