hello all

i dont know what is the problem ....... a very simple change in the order of functions make the program goes in infinite loop

this code works will

#include <iostream>
using namespace std;
int main()
{
    int i;
    while(true)
    {
        cout << "\nEnter Intger:";
        cin >> i;
        if (cin.good())
        {
            break;
            cin.sync();
        }
        cin.clear();
        cin.sync();
        cout << "\nError\n";
    }
    cout << "\nYou Entered :"<<i;
    return 0;
}

but when i swap cin.sync and cin.clear
like that

#include <iostream>
using namespace std;
int main()
{
    int i;
    while(true)
    {
        cout << "\nEnter Intger:";
        cin >> i;
        if (cin.good())
        {
            break;
            cin.sync();
        }
        cin.sync();
        cin.clear();
        cout << "\nError\n";
    }
    cout << "\nYou Entered :"<<i;
    return 0;
}

infinite loop happens

why ?? and please if any one has a link that takes about buffer please give it to me

Tried you code out in GCC 4.1.2, no problems, same output for both cases, no infinite loops!!
I had directly copy pasted your code.

strange .......... i use mingw

did you entered chars not ints ??

strange .......... i use mingw

did you entered chars not ints ??

I think that was an important information u left out ;)
Yep, it does go into the infinite loop, but so does the first one.

But why exactly its occurring I cant figure it out, I hope some of the other guys know of it.

if (cin.good())
{
  break;
  cin.sync();
}

The sync call will never be reached in this snippet because break leaves the loop immediately.

>why ??
Because when you type a value that cin doesn't expect (ie. a non-integer character when an integer is expected), cin goes into an error state. When cin is in an error state, all input requests do nothing until you clear the state.

What your first example does (in theory) is clear the state and then discard all of the characters in the stream so that you wipe the slate clean and can try again. The second example tries to discard all of the characters in the stream first, even though the stream is still in an error state and the sync call is effectively a no-op because of it.

I say "in theory" because cin.sync() isn't required to flush the input stream.

commented: Doin' the safety dance! +18

thanks very very much

>I say "in theory" because cin.sync() isn't required to flush the input stream.

yes i read your thread .......thanks again

i know i ask alot but plz any one answer me

why this

cout<<cin.rdbuf();

makes a loop

Why not?

:D :D

what i know that cin.rdbuf returns all what in the buffer

then cout will show the buffer that is returned

but why loop happens
and if you can explance more cin.rdbuf plzzz

>what i know that cin.rdbuf returns all what in the buffer
Kind of. It returns a pointer to the buffer object.

>but why loop happens
It's library magic. The << operator reads everything it can from the object it's given, and the buffer object will return characters from the attached stream until end-of-file. Together this "creates a loop", if that's how you want to describe it.

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.