943,604 Members | Top Members by Rank

Ad:
  • C++ Tutorial
  • Views: 68485
  • C++ RSS
May 26th, 2008
-2

Re: How do I flush the input stream?

>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.
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
May 26th, 2008
0

Re: How do I flush the input stream?

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.
Light Poster
iansane is offline Offline
26 posts
since Mar 2008
May 27th, 2008
2

Re: How do I flush the input stream?

> ...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.
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
May 27th, 2008
1

Re: How do I flush the input stream?

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.
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
May 27th, 2008
0

Re: How do I flush the input stream?

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
Light Poster
iansane is offline Offline
26 posts
since Mar 2008
Jun 13th, 2008
-1

Re: How do I flush the input stream?

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
Newbie Poster
xxxviking is offline Offline
19 posts
since Jun 2008
Nov 19th, 2008
0

Re: How do I flush the input stream?

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, 102 views)
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Dec 8th, 2008
0

Re: How do I flush the input stream?

C++ string and getline are your friends so get to know them well

CPP Syntax (Toggle Plain Text)
  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.
Newbie Poster
ronjustincase is offline Offline
12 posts
since Dec 2008
Dec 17th, 2008
-1

Re: How do I flush the input stream?

cout.flush ????
Newbie Poster
AHUazhu is offline Offline
17 posts
since Dec 2008
Dec 17th, 2008
0

Re: How do I flush the input stream?

Click to Expand / Collapse  Quote originally posted by AHUazhu ...
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
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Message:
Previous Thread in C++ Forum Timeline: redefine equlity of pairs
Next Thread in C++ Forum Timeline: Check it out again guys





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC