Thread: How to clear screen after short pause?

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    6

    Question How to clear screen after short pause?

    I use fflush(stdout) but it doesn't work. The screen clears too soon before my eyes.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    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");

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anonymous1
    I assume that my screen will pause for a few seconds before screen clears
    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.

    Quote Originally Posted by anonymous1
    but the fflush(stdout) doesnt work
    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."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    Sry, i'm new to programming. Anyway, thanks for your explanation.

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    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.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Avoid using system(). Since you are using Windows, use the Console API do clear the screen:
    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);
    }
    And, on Windows you can use Sleep() to wait N milisseconds.

  8. #8
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    Quote Originally Posted by hamster_nz View Post
    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.
    Thank you!👍👍

  9. #9
    Registered User
    Join Date
    Nov 2020
    Posts
    6
    Quote Originally Posted by flp1969 View Post
    Avoid using system(). Since you are using Windows, use the Console API do clear the screen:
    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);
    }
    And, on Windows you can use Sleep() to wait N milisseconds.
    Thank you so much!👍👍

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to clear screen after short pause
    By dunsta in forum C Programming
    Replies: 18
    Last Post: 04-28-2010, 06:20 AM
  2. How to clear screen?
    By logicwonder in forum C++ Programming
    Replies: 10
    Last Post: 01-04-2006, 06:49 AM
  3. Pause/slow a screen?
    By iharringron in forum C++ Programming
    Replies: 7
    Last Post: 02-15-2003, 12:09 AM
  4. clear screen
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-04-2002, 12:50 PM
  5. Clear Screen
    By evilmonkey in forum C++ Programming
    Replies: 6
    Last Post: 11-07-2001, 06:18 PM

Tags for this Thread