Can someone help me using fflush
in C++
Here is a sample code in C
#include <stdio.h>
using namespace std;
int a,b,i;
char result[20];
int main() {
scanf("%d %d\n", &a, &b);
for (i=1; i<=10; i++) {
printf("5\n");
fflush(stdout);
gets(result);
if (strcmp(result, "congratulation") == 0) break;
}
return 0;
}
This is program for getting interactive input.
I usually use cin
and cout
so is it possible not using printf
and scanf
?
Although I haven't completely understood your question, the C++ version of your program would be something like this (assuming
hasil
should beresult
):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 yourfflush
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:If you have need for C IO facilities, include
<cstdio>
. You now havestd::printf
andstd::fflush
etc. You might consider callingstd::ios::sync_with_stdio()
if you want to use C IO and iostreams interwovenly.The translation to C++ programming style is this:
Note that I deliberately added some error checking.