Thread: why is this skipping an input field?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    3

    why is this skipping an input field?

    Hi all, I'm new to the forum and am working threw Mr. Allain's book to refresh/re-learn from my previous programming classes that I took a few years ago.

    In this simple end-of-chapter problem, the ages of two people are to be compared and determine who's older. The problem I'm experiencing is that the second user name input is being skipped and going straight to the second users age.

    So while running the program it looks something like:

    What is user ones name?
    <input name>
    How old are they?
    <input age>

    What is user twos name?
    How old are they?
    <input age>

    <if statement result>


    Here's my code:
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int user_one_age;
    int user_two_age;
    string user_one_name;
    string user_two_name;
    
    
    int main()
    {
            ///Get the user names and their ages
            cout << "What is user ones name?" << endl;
            getline( cin, user_one_name, '\n' );
            cout << "How old are they?" << endl;
            cin >> user_one_age;
    
    
            cout << "\nWhat is user twos name?" << endl;
            getline( cin, user_two_name, '\n');
            cout << "How old are they?" << endl;
            cin >> user_two_age;
    
    
            if ( user_one_age > user_two_age )
            {
                cout << "\n\n" << user_one_name << " is older than " << user_two_name << endl;
            }
            else
            {
                cout << "\n\n" << user_two_name << " is older than " << user_one_name << endl;
            }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Mixing getline and plain old >> is often a bad idea. In this case, the >> on line 20 leaves enter-key in the input stream (since it only grabs the number), which means that line 24 doesn't actually pick up any data.

    If you are willing to deal with names that don't have spaces, you can turn the getline into >> operations as well; otherwise, you will need to use something like ignore after line 20 to peel off that extra newline.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    3
    Thank you for the quick response, so I assume then that this statement (direct from the book) is not true after all? or is it something else I'm not understanding?

    "There is a special function, getline, which can be used to read in the whole line. It will even automatically discard the newline character at the end."

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kjorin View Post
    Thank you for the quick response, so I assume then that this statement (direct from the book) is not true after all? or is it something else I'm not understanding?

    "There is a special function, getline, which can be used to read in the whole line. It will even automatically discard the newline character at the end."
    getline does discard the \n character. However, the implication therefore is that if you don't use getline, the \n character is not discarded. So on line 20, when you use >> instead of getline, the \n is not discarded, which brings us trouble in line 24.

    To put it another way:
    • >> ignores \n at the beginning, but leaves \n at the end
    • getline does not ignore \n at the beginning, but removes it from the end

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tabstop View Post
    • >> ignores \n at the beginning, but leaves \n at the end
    istream's operator>> stops when '\n' is the next character waiting to be read (which is not ignoring it), and does not read it (which leaves the '\n' at the end, as you say).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    3
    Ohh, I follow now.

    Thank you both for answering and sufficiently explaining such a simple thing for me.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by grumpy View Post
    istream's operator>> stops when '\n' is the next character waiting to be read (which is not ignoring it), and does not read it (which leaves the '\n' at the end, as you say).
    Eh? If that was true, >> would be completely impossible to use more than once in a program (since the second input could never possibly happen), and that seems pretty useless. Fortunately, that's not true.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tabstop View Post
    Eh? If that was true, >> would be completely impossible to use more than once in a program (since the second input could never possibly happen), and that seems pretty useless. Fortunately, that's not true.
    I didn't argue with your comment about operator>> skipping whitespace at the beginning.

    Incidentally, it is whitespace that is a special case. If the input contains "23X" the 'X' will prevent reading any more ints until it is removed. That does result in an infinite loop in code like.
    Code:
    while (!(some_stream >> some_int)) 
    {
         // do something with some_int
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by grumpy View Post
    I didn't argue with your comment about operator>> skipping whitespace at the beginning.
    ... sure. (I have no idea what else "not ignoring it" could refer to, since that was the only place I used the word "ignore", but I'll take your word for it.)

  10. #10
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    If this issue happenes when you use getline an easy fix is to use std::cin.ignore()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to specify the input field.
    By paradize in forum C Programming
    Replies: 6
    Last Post: 05-02-2006, 01:50 PM
  2. Program skipping some input
    By Dita in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2004, 03:08 PM
  3. Skipping input...
    By sirSolarius in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 09:23 PM
  4. skipping input?
    By axon in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 01:32 PM
  5. skipping input
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2002, 01:22 PM