Thread: Wierd Problem with inputting.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    30

    Wierd Problem with inputting.

    In the following code why does the getline statement get skipped in the first iteration.

    Code:
    vector<string> names(r);
    
    	for(i=0;i<r;i++)
    	{
    		getline(cin,names[i],'\n');
    		cout<<i<<endl;
    
    		fflush(stdin);//does not help
    		fflush(stdout);//does not help
    
    	}
    Now if I add "getline(cin,names[0],'\n');" just before the loop the problem is solved.
    what do you think is the problem and is there a an alternative to the workaround I came up with.

    Also what is the best way to input a string into an 1)array of chars(other than gets)
    2)stl string (multiple words)

    Hope someone figures this out.
    Cheers!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you have some code before the loop, in particular code that uses formatted input that leaves a newline in the buffer? (e.g., cin >> x)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Using stdio functions such as fflush() should not be mixed with iostream functions. [At least not without using sync_with_stdio()]
    2. fflush(stdin) is undefined behaviour. It may work in some implementations, and crash in others.
    3. Is that the complete application, or is there, in your real code, something like cin >> x before your getline(). In that case, you will need a cin.ignore() before the getline().

    4. cin.getline() and std::getline() are the best ways to read into a char array and std::string respectively.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::getline is better since you do not have to worry about ensuring the buffer is big enough to hold the data.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    std::getline is better since you do not have to worry about ensuring the buffer is big enough to hold the data.
    Just to clarify: using std::string is better in that respect than char arrays - because char arrays assume that you KNOW how big a string you need.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    More fundamentally, if r is zero, the body of the loop will not be executed.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    30
    Quote Originally Posted by matsp View Post
    1. Using stdio functions such as fflush() should not be mixed with iostream functions. [At least not without using sync_with_stdio()]
    2. fflush(stdin) is undefined behaviour. It may work in some implementations, and crash in others.
    3. Is that the complete application, or is there, in your real code, something like cin >> x before your getline(). In that case, you will need a cin.ignore() before the getline().

    4. cin.getline() and std::getline() are the best ways to read into a char array and std::string respectively.

    --
    Mats
    Thanks to all of you (Mats in particular)!

    cin.ignore() worked perfectly.(yes I did have a cin>>x statement).

    Thanks once again ,I learnt something new .
    cheers!
    ps:In general what are the situations u generally use cin.ignore()?
    Last edited by eklavya8; 05-30-2008 at 11:30 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    After cin >> x.
    Or you can always use std::getline and use a stringstream to convert into appropriate data type.
    It's funny how C++, even though supposed to have evolved from C, still suffers the very same problem as C, and this having to use the exact same approach as in C to solve it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> In general what are the situations u generally use cin.ignore()?

    cin.ignore() ignores the next character in the input stream. It is generally used in beginner programmers to ignore a leftover newline.

    The leftover newline comes from when you user operator>> to read in data. The user types in their input and hits <enter>, then the input is read in with cin >> but the newline from the <enter> is still in the input stream. You would then call cin.ignore() to remove that newline.

    A simple strategy is to always call cin.ignore() after you call cin >> to read in data. Do not call cin.ignore() after you call getline because getline ignores that newline automatically, and so the cin.ignore() would ignore a valid character.

    That simple strategy doesn't work perfectly if the user types in bad input or other extra characters, but assuming the user enters correct input it should work well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM