flushing stdin is undefined behavior, since fflush is meant to be called on an output stream. This is taken from the C standard [7.21.5.2]/2:
int fflush(FILE *ostream);If stream points to an output stream or an update stream in which the most recent operation was not input, the
fflushfunction causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
you can find the standard C here in the link below, go to page 139
ANSI/ISO 9899-1990
Answer from AdamF on Stack OverflowHow to use fflush(stdin) in C++ - Stack Overflow
input - using fflush on C++ - Stack Overflow
C++ - Why fflush(stdout) does not work with iostream? - Stack Overflow
Difference between std::flush and std::fflush
Videos
The translation to C++ programming style is this:
#include <iostream>
using std::cin;
using std::cout;
using std::string;
int main() {
string line;
int a, b;
if (cin >> a >> b) {
for (int i = 0; i < 10; i++) {
cout << "5" << std::endl; // endl does the flushing
if (std::getline(cin, line)) {
if (line == "congratulations") {
break;
}
}
}
}
return 0;
}
Note that I deliberately added some error checking.
Although I haven't completely understood your question, the C++ version of your program would be something like this (assuming hasil should be result):
#include <iostream>
int main() {
int a,b,i;
std::string result;
std::cin >> a >> b;
for (i=1; i<=10; i++) {
std::cout << "5" << std::endl;
std::cin >> result;
if (result == "congratulation") break;
}
return 0;
}
Note, that std::endl is equivalent to '\n' << std::flush and therefore both puts the line end and calls .flush() on the stream (which is your fflush equivalent).
Actually to get the real equivalent to your scanf call (and not press enter between a and b), you would have to do something like:
#include <sstream>
...
std::string line;
std::cin >> line;
std::istringstream str(line);
str >> a >> b;
My first question is "why would you want to"? There is a std::ostream::flush() function for this purpose, so use that e.g. cout.flush();.
The reason that it "doesn't work" is that the buffer which is flushed with fflush(FILE* f) is not the same buffer that is used for std::ostream (or at least it's not guaranteed that it will be). It's quite likely that std::ostream::flush() does call fflush(FILE*) on the underlying file object that is part of the implementation.
The purpose of the calls ios::sync_with_stdio(false) and cin.tie(0) that you have put in "on purpose" is to ensure (1) that C I/O streams (stdout, etc) are not synchronised with their C++ counterparts (std::cout, etc) and (2) to ensure that stdout and stdin are not tied (i.e. reading from stdin does not necessarily cause stdout to be flushed).
That is why fflush(stdout) does not affect std::cout in your example. You have specifically disabled such an effect, and the two may be buffered separately.
The effect of freopen() on any C++ stream that may be synchronised with the supplied file handle is undefined. In practice, there may be some buffers used in common, which explains what you are seeing - at least, with your compiler/library. But that behaviour is not guaranteed with other implementations.
Is there a difference between std::flush and std::fflush?
For example:
std::cout << "some text\n" << std::flush;
Vs
std::cout << "some text\n"; std::fflush( stdout );
Do the above statements perform the same things?
Does std::cout have its own buffer so that calling std::flush makes it flush to the stdout and then it calls std::fflush( stdout ) to flush stdout to the associated output device? Or does it write directly to the stdout without std::flush being used?