Thread: Skipped Input (resolved but elaboration would be nice)

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Skipped Input (resolved but elaboration would be nice)

    I was about to ask why the following code was skipping question three but I changed the getline statement to a cin and everything is fine now. I was hoping someone could explain why getline doesn't work on single answer responses so I understand the underlying principles though.

    Code:
    int age, pushups;
    string name, military;
    char gender;
    cout << "What is your full name? "; getline (cin, name);
    cout << "How old are you? "; cin >> age;
    cout << "Were you ever in the military (yes/no)? "; getline (cin, military);
    cout << "How many pushups can you do in a row? "; cin >> pushups;
    cout << "Are you (m)ale or (f)emale? "; cin >> gender;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cin >>
    This ignores \n at the start of an input, and leaves \n behind at the end of an input.

    getline() stops at the first \n it sees.

    Mixing the two together usually ends up with trouble.

    cin.ignore() is one remedy.

    Using getline() for EVERYTHING is another way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Does getline() work for all variables? I had only seen it used in conjunction with string variables and assumed that was all it was used for. Thanks for the elaboration by the way, all you guys on this forum really help me learn this language.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    getline only works with char arrays and std::string

    What you do with the string once you've got it is up to you.
    One useful thing to use is stringstream
    stringstream - C++ Reference

    It might seem to be the long way, but by the time you've added
    - input error detection
    - input error recovery
    - conversion error detection
    - conversion validation
    then all the possible routes you could take generate about the same amount of code.

    Take something like
    int a;
    cin >> a;

    What would you do if the user entered "hello" instead?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  2. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  3. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  4. input files in C
    By LWisne in forum C Programming
    Replies: 4
    Last Post: 09-30-2002, 06:24 AM
  5. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM