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);
}

You usually have to clear the input buffer

  1. After entering numeric data, such as short, int, long, float and double
  2. After using cin >> input operator because it stops at the first space
  3. 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.

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
}

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;

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.