Wierd Problem with inputting.

This is a discussion on Wierd Problem with inputting. within the C++ Programming forums, part of the General Programming Boards category; 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++) { ...

  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
    21,727
    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)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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,297
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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,318
    >> 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, 05:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21