like in c,we use fflush(stdin) to flush input stream. but this doesnt work in c++. plz tell me any function or any way by which i can flush the input stream in c++.
Four answers:
Techwing
2011-01-14 20:26:18 UTC
Use cout.flush(). or cout ... << flush.
husoski
2011-01-14 20:17:51 UTC
I think the best you can do in standard C++ is to use istream::readsome(). This will read characters as the stream knows it can read without blocking. The only problem is that the stream may be built on an underlying OS/Platform that doesn't have a function to tell whether the next character read will block or not.
This function seems to work on Windows, using Visual C++ 2008, but fails with Ubuntu Linux and GNU G++ 4.4.3
// Flush input from given stream, with optional display.
void iflush(istream& istr, bool display=false)
{
const int MAX = 32;
char buf[MAX];
int n;
while ((n=istr.readsome(buf, MAX)) > 0)
{
if (display)
{
string x(buf,n);
cout << "Flushed: " << x << endl;
}
}
}
?
2011-01-14 21:45:21 UTC
Who is that "we" that uses fflush(stdin) in C?
That function call is an error. Or, to be exact, it is Undefined Behavior, per C programming language standard ISO 9899:1999 paragraph 7.19.5.2/2.
There is no function to "flush" an input stream in either language. What does that even mean? If you mean "ignore all input until the end of file", it is cin.ignore(std::numeric_limits ::max() ); If you mean "ignore all input until the next line", it is cin.ignore( std::numeric_limits ::max(), '\n' );
anonymous
2016-10-28 11:50:25 UTC
The project is this line std::getline (std::cin, sentence); switched over to std::cin >> sentence; answer: #contain iostream #contain cstring int maximum significant ( ) char decision = ' '; std::string sentence = " "; do //#a million std::cout << "Please enter your sentence, ending with a era(.): " << std::flush; std::cin >> sentence; //std::getline (std::cin, sentence); if (sentence.length() > one hundred length() - a million, a million) != ".") std::cout << "blunders lacking '.' or exceeded quantity of characters" << std::endl; else //#2 std::cout << "Your sentence has... " << std::endl; std::cout << "words: " << std::endl; std::cout << "Characters (no areas): " << std::endl; std::cout << "Characters (with areas): " << sentence.length() << std::endl; std::cout << "The letter 'a'count: " << sentence.to discover('a', 0) << std::endl; //#3 std::cout << "Press any key to proceed...('s' to give up): " << std::flush; std::cin >> option; even as (option != 's'); go back (0);//end
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.