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
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
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