I use fflush(stdout) but it doesn't work. The screen clears too soon before my eyes.
I use fflush(stdout) but it doesn't work. The screen clears too soon before my eyes.
Well the first thing is that fflush(stdout) doesn't do anything remotely like clearing the screen.
What it does do is ensure that any local output buffer (local to the program) is forwarded onto the operating system.
Secondly, clearing the "screen" presumes that the stdout is actually connected to a screen that can be cleared.
You simply have to redirect the output (say myprog > file) and you no longer have a screen.
Even when you have a screen, how you clear it is greatly dependent on your choice of operating system and choice of terminal.
ANSI escape code - Wikipedia
You could try your hand with
printf("\033[2J");
fflush(stdout);
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Sorry i forgot to say that i use fflush(stdout) to clear the output buffer and i use system("cls") to clear the screen. I assume that my screen will pause for a few seconds before screen clears, but the fflush(stdout) doesnt work. This is part of my code,
printf("customer has to pay:");
printf("%d\n", price);
fflush(stdout);
total = total + price;
a++;
system("cls");
Your assumption is incorrect. If you want such a pause, you need to insert relevant code to make that happen. This is not part of the C standard, so exactly how to do so is system-dependent.Originally Posted by anonymous1
That's a bit like saying: "I put my clothes in the washing machine and turned it on. I assume that the dishwasher will clean the dishes, but the washing machine doesn't work because the dishes aren't clean."Originally Posted by anonymous1
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Sry, i'm new to programming. Anyway, thanks for your explanation.
If you are on Linux there is the "sleep(x)" function that will cause a program to sleep for x seconds. You will need to include the unistd.h header file for the function prototype.
Avoid using system(). Since you are using Windows, use the Console API do clear the screen:
And, on Windows you can use Sleep() to wait N milisseconds.Code:#include <windows.h> ... void clearscreen(void) { HANDLE hConsole; COORD coord = {0, 0}; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConsoleSize, dwWriten; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConsoleSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, (TCHAR)' ', dwConsoleSize, coord, &dwWriten); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConsoleSize, coord, &dwWriten); SetConsoleCursorPosition(hConsole, coord); }