in c i would
while(c == getchar()) != '\n');
but how do i do this in c++?
in c i would
while(c == getchar()) != '\n');
but how do i do this in c++?
'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition
cin.clear();
cin.clear() just clears the errorbit evaluated by cin.good() and cin.fail(). It doesn't clear the input buffer. In short there is no great way to clear the input buffer that I know of. Some people usie cin.ignore() with large ints. Some people use a loop reading all data in buffer to dummy variable and ignore it (they address the buffer directly with something like rdbuf()).
you were close....
this is one way....
while(getchar() != '\n'); // notice semi colon
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Code:while (cin.rdbuf()->in_avail() > 0) cin.get();
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Hammer, that code doesn't work in CodeWarrior C++.
Works fine in MSVC++, though.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
That's the only bad thing about C++, IMO.Originally posted by elad
In short there is no great way to clear the input buffer that I know of.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
... and there was me trusting one of your previous posts (yes, I cut paste that line from here)Originally posted by Sang-drax
Hammer, that code doesn't work in CodeWarrior C++.
Works fine in MSVC++, though.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Heh, at that time I thought my method was good.
I discovered the CodeWarrior problem when I tried it yesterday.
I don't know what the standard has to say about in_avail(), but in CW it always seems to return 0.
But there must be some way to clear the buffer!
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Well, you can always stick with cin.ingore
>cin.ignore(INT_MAX, '\n')
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I think one of the problems is different i/o operations rely on the state being in a certain condition (such as whether a '\n' is already present in the buffer or not). But isn't
while ((c = cin.get())!='\n');
the equivilent of
while((c = getchar()) != '\n');
Joe
How to implement func() so that cin ALWAYS is empty before reading in var2?Originally posted by Hammer
Well, you can always stick with cin.ingore
Code:void func(); int main() { if (randomcondition) { int var; cout << "var:"; cin >> var; } func(); int var2 cout << "var2"; cin >> var2; }
Assuming the in_avail() method is not standard, here's an imperfect version:
It won't work if the user enters "123 456" in the first cin>>Code:void func() { if (cin.eof()) cin.clear(); else if (cin.fail()) { cin.clear(); cin.ignore(10000,'\n'); } else if (cin.bad()) cin.clear(); }
Last edited by Sang-drax; 10-04-2002 at 06:14 PM.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
are they equivilent?Originally posted by JoeSixpack
But isn't
while ((c = cin.get())!='\n');
the equivilent of
while((c = getchar()) != '\n');
'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition