Clear screen function

What should be used in c++ for clrscr()? as we us in c
When I put clrscr(); after variable declaration in program it is showing error
and error is that function should have a prototype.
Yes I ask for dos based console only
Thank u every body for information and I am a beginner in C and have started in dos based console borland turbo C
I think beginner forum is good for me and I asked this question bcoz it was showing output after the 1st program's o/p on same screen?
Last edited on
There isn't one. Why do you need to clear the console?
There isn't one, because the question is: Which console?

There is no the console. There is no standard. There are multiple, different terminal types, which each accept different control events. The clsscr() was for a DOS-based console.

Look up ncurses. It is a more recent library that implements some "text UI". There is some variant *curses for Windows too.
Last edited on
There is no standard. For Windows you can use;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <windows.h>

void clear_screen()
{
  DWORD n;                         /* Number of characters written */
  DWORD size;                      /* Number of visible characters */
  COORD coord = {0};               /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}
heres something i wrote based off duoas's post (well more like threw together. i didnt actually write it)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifdef _WIN32
    #include <windows.h>

#else
    #include <unistd.h>
    #include<term.h>
#endif

    void clear() {
    #ifdef _WIN32
        HANDLE                     hStdOut;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD                      count;
        DWORD                      cellCount;
        COORD                      homeCoords = { 0, 0 };

        hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

        if(hStdOut == INVALID_HANDLE_VALUE || !GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;

        cellCount = csbi.dwSize.X *csbi.dwSize.Y;

        if(!FillConsoleOutputCharacter(hStdOut, (TCHAR) ' ', cellCount, homeCoords, &count)) return;
        if(!FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, cellCount, homeCoords, &count)) return;

        SetConsoleCursorPosition( hStdOut, homeCoords );

    #else
        if(!cur_term) {
            int result;
            setupterm( NULL, STDOUT_FILENO, &result );
            if(result <= 0) return;
        }

        putp(tigetstr("clear"));
    #endif
    }

Why do you need to clear the console?

there are many reasons why... console programs arent irelevant
Last edited on
You aren't required to clear the screen if you make a console program.
i am aware. that doesnt mean you dont need to. im also not required to clear my gui irc screen. but i do anyways. im not required to clear my browsing history. but i do anyways
Topic archived. No new replies allowed.