How do I flush the input stream?

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: How do I flush the input stream?

 
0
  #11
May 26th, 2008
>Again if anyone knows why I'm getting the compile error posted above when
>using g++ in linux (ubuntu) would you please help me out?

I had nothing better to do, so I decided to screw around with the code Narue posted. You're correct in that g++ gives an error when compiling the 'pause' class. Try renaming the entire 'pause' class to something else, perhaps pause2 (in other words, do an entire find and replace with 'pause'), and it'll compile fine. I asked Narue about this on IRC, and she figured it was probably some gcc/g++ extension that was messing things up (which is obviously the only real possibility here).
Last edited by John A; May 26th, 2008 at 7:52 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

Re: How do I flush the input stream?

 
0
  #12
May 26th, 2008
Thank you John A and Narue.

I never would have thought of something like that since I'm a noob. I don't know enough to decide between windows, linux and the different compilers who is complying with c++ standards and who is not fully compliant. You are a real help.
Thanks for the hard work coding this and thanks for looking into my g++ problem.
Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: How do I flush the input stream?

 
1
  #13
May 27th, 2008
> ...probably some gcc/g++ extension that was messing things up
> (which is obviously the only real possibility here).
it is not some gcc/g++ extension; it is POSIX
man pause http://node1.yo-linux.com/cgi-bin/ma...ommand=pause(2)
Last edited by vijayan121; May 27th, 2008 at 2:39 am.
Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: How do I flush the input stream?

 
1
  #14
May 27th, 2008
That's why Edward puts as much code as possible in namespaces. Compilers are bad about including all kinds of unexpected stuff in the standard headers, so even if you don't include unistd.h yourself, iostream or another standard header might do it for you and create a name conflict.
If at first you don't succeed, keep on sucking until you do succeed.
Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

Re: How do I flush the input stream?

 
0
  #15
May 27th, 2008
I can not find unistd.h anywhere on my system.

So let me see if I understand this.

POSIX is the linux API just like there's a Windows API.

By changing pause to pause2, we have something that complies to c++ standards, Windows API standards, and POSIX standards.

Is that how it works? I guess just learning c++ will not be enough if I want to program cross platform?

Is one or the other of the above mentioned not 100% following standards or is that just the way it works?

Sorry so many questions but I don't understand this type of issue yet. Tell me if I'm way off in my way of thinking. Is this even a standards issue or something else?

Thanks
Quick reply to this message  
Join Date: Jun 2008
Posts: 19
Reputation: xxxviking is an unknown quantity at this point 
Solved Threads: 0
xxxviking xxxviking is offline Offline
Newbie Poster

Re: How do I flush the input stream?

 
-1
  #16
Jun 13th, 2008
hmm nice topic. i leart the way...thanks to all..
Last edited by Narue; Jun 13th, 2008 at 9:53 am. Reason: Snipped fake signature
Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: How do I flush the input stream?

 
0
  #17
Nov 19th, 2008
Don't know how useful this is to anyone else, but I made a pdf set up for easy reading on my Irex Iliad digital book reader. I'm sure some of you have some Kindles or Sony Readers or whatever.

Anyway, here you go, my pdf I made of Narue's post. (By the way if you feel this is in some way a violation of your work feel free to remove it)
Attached Files
File Type: pdf flush_in_stream.pdf (490.7 KB, 21 views)
I would love to change the world, but they won't give me the source code
Quick reply to this message  
Join Date: Dec 2008
Posts: 12
Reputation: ronjustincase is an unknown quantity at this point 
Solved Threads: 2
ronjustincase ronjustincase is offline Offline
Newbie Poster

Re: How do I flush the input stream?

 
0
  #18
Dec 8th, 2008
C++ string and getline are your friends so get to know them well

  1. // flushinput.cpp
  2. // A test program to test flushing input specifically cin
  3. // Last Modified: 20081208
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include <iomanip>
  9. #include <exception>
  10. #include <stdexcept>
  11. #include <new>
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16. cout << "flushinput" << endl;
  17. cout << "A test program to test flushing input specifically cin." << endl;
  18. string input1 = "";
  19. string input2 = "";
  20. cout << endl;
  21. cout << "There are 5 input tests:" << endl;
  22. cout << "1. Type 1 or more characters and then press Enter." << endl;
  23. cout << "2. Just hit Enter(don't type any characters, just press Enter)." << endl;
  24. cout << "3. Signal EOF(don't type any characters, Linux - just press Ctrl+D. Windows - just press Ctrl+Z and then Enter)." << endl;
  25. cout << "4. Type 1 or more characters and then Signal EOF and then press Enter(Windows - press Enter a second time)." << endl;
  26. cout << "5. Type 1 or more characters and then Signal EOF twice(Windows - press Enter twice after signalling EOF twice)." << endl;
  27. cout << endl;
  28. cout << "Correct solution would be that the program waits for you to enter the 2nd input regardless of what you entered for the 1st input." << endl;
  29. cout << "Incorrect solution would be that the program finishes without asking for the 2nd input." << endl;
  30. cout << endl;
  31. cout << "Enter 1st input: ";
  32. getline(cin, input1);
  33. cerr << endl;// Input tests 1 - 5: L - Linux, W - Windows L L L L LW
  34. cerr << "cin.bad()=" << cin.bad() << endl;// Results: 0 0 0 0 00
  35. cerr << "cin.eof()=" << cin.eof() << endl;// 0 0 1 0 10
  36. cerr << "cin.fail()=" << cin.fail() << endl;// 0 0 1 0 00
  37. cerr << "cin.good()=" << cin.good() << endl;// 1 1 0 1 01
  38. cerr << "input1.size()=" << input1.size() << "!" << endl;// 3 0 0 3 33
  39. if(!cin.good())// Input tests 3(Windows & Linux) & 5(Linux only) put cin into an error state.
  40. {
  41. cin.clear();// Reset the cin status flags to their default.
  42. }
  43. cout << endl;
  44. cout << "Enter 2nd input: ";
  45. getline(cin, input2);
  46. cout << "Program finished." << endl;
  47. return 0;
  48. }

There are some minor differences between Linux and Windows which I have documented. For input tests 1, 4, & 5 I used:
abc
for the 1st input.
Quick reply to this message  
Join Date: Dec 2008
Posts: 17
Reputation: AHUazhu is an unknown quantity at this point 
Solved Threads: 2
AHUazhu AHUazhu is offline Offline
Newbie Poster

Re: How do I flush the input stream?

 
0
  #19
Dec 17th, 2008
cout.flush ????
Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How do I flush the input stream?

 
0
  #20
Dec 17th, 2008
Originally Posted by AHUazhu View Post
cout.flush ????
This is a discussion about flush the INPUT buffer....so this is a completely pointless post, before making a suggestion at least make it a viable suggestion

Chris
Knowledge is power -- But experience is everything
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC