954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

flushing the input stream

hi narue i would like to ask if this method should be called after ever cin statement? the reason i ask this is that if i write a program and i have 2 cin >> text; statements then at the end before my return 0; i have to use 2 cin.get(); statements to clear the newlines left in the buffer. i was thinking of trying a loop to eat everything but i don't know if that would cause problems.

char ch;
while (!std::cin.eof())
{
        cin.get(ch);
}
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

You usually have to clear the input buffer After entering numeric data, such as short, int, long, float and double
After using cin >> input operator because it stops at the first space
After using a character array that is smaller than the number of characters typed at the keyboard

The code you posted is an infinite loop unless you press Ctrl+Z at they keyboard.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
i would like to ask if this method should be called after ever cin statement?


For anyone else reading, the method in question is here . This thread used to be a post in that thread.

operator>>() is the problem child here. It works something like this:

// beware: pseudo-code
ostream& operator>>(ostream& os, T obj)
{
    SkipWhiteSpace(os);
    ReadObject<T>(obj);
    return os;
}

Any white space after the object will not be touched until the next call to operator>>(). If the stream has " abcd\nefgh" , here is what happens:

cin >> s1;        // s1 == "abcd"
getline(cin, s2); // s2 == ""
getline(cin, s3); // s3 == "efgh"

operator>>() does its thing by skipping leading white space and reading the object. It stops on white space thinking that the next call to operator<<() will skip it. But getline does not skip leading white space, and it even uses '\n' as a stopping character by default.

You only need to use the method from that thread in cases where the next input call will give you the finger. ;) A good guideline is clear the stream after the formatted input whenever you mix formatted and unformatted input.i was thinking of trying a loop to eat everything but i don't know if that would cause problems.
A loop is good, but it needs to stop on '\n' too, and using eof() in the loop condition should be avoided because it has the wrong behavior and there is a shorter way of doing it:

char ch;

while (cin.get(ch) && ch != '\n')
{
    // all word done in condition
}

A better way is to avoid the problem by using unformatted input exclusively. Always use getline() and string objects to read the whole line from the stream, then work with the string in memory. That leaves the stream in a clean state at all times and it's easier to work with. :)

You can still get the same formatting goodness from a stream without breaking cin by using string streams:

string line;

if (getline(cin, line))
{
    istringstream is(line);

    // use is like cin
}
Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

so to avoid problem later on with information in the stream i should either clear the stream after calling cin >> or use strings with cin.getline() .
one other question is will there be a newline in the buffer after running this code?

int a;
cout << "please enter an integer: ";
cin >> a;
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

>>will there be a newline in the buffer after running this code?
Yes, and I think we have already explained that.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

well thank you guys for your help. it is really appreciated.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You