Thread: String input reliability question

  1. #1
    Registered User
    Join Date
    Jun 2007

    String input reliability question

    Well first off I'd like to say that this is an amazing site. An absolute treasure trove of information.

    Searching through the trove though, I didn't find an answer to my question. Hopefully someone can shed some light on it.

    I am a beginner at C++, but would like to know what would be the most reliable way to have a string input from the user.

    Using cin >> is no good, since it is delimited by whitespace.

    I used getline(), but for some reason the user has to input two new lines before advancing in the program.

    A solution to this is to use cin.getline() and have it saved into a char[], but the main problem with both getline functions is if the user immediately presses enter everything goes to hell. Something at least cin >> could deal with.

    So my solution has been:

    	    cin.getline(mystring, 30);   
    	} while ( mystring[0] == '\0' );
    Unlike cin of course it does read past whitespace, and in case the user presses enter as first input the request is repeated.

    How is this problem normally tackled? What could be done better?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    There are a couple of possible causes. If you use VC++ 6.0, there is a bug in the string version of getline that causes you to have to hit enter twice.

    Another more common problem is if you use both operator >> and getline in the same program. When you read with operator>>, it does not read in any trailing whitespace. This means that when the user types an answer and hits <enter>, a newline is left in the stream. If you then call getline, the getline function sees that newline and stops.

    The solution is to ignore the newline after you read in with operator>>. The simplest method is to call cin.ignore() after every call to cin >>. This is safe whether you use cin >> again or getline. Do not call cin.ignore() immediately after getline, though, since getline already ignores that trailing newline for you.

    BTW, it looks like you are using C style strings (char[]) in your program. There is very little reason to do that in C++. Use the C++ string class instead. It has its own version of getline:
    getline(cin, mystring);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > I used getline(), but for some reason the user has to input two new lines before advancing in the program.
    The probable cause is that you have a previous cin in the code, and the first getline is just dealing with the trailing newline left behind

    Use getline for all input, then parse / validate / convert that information from the memory buffer.

    Use std::string wherever you can, not char arrays.
    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. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 01:33 PM