clear screen

eagleeye 0 Tallied Votes 354 Views Share

A simple demostration of how to clear the console screen in at least Dev-C++ (I don't know about the other since I've only tried it on this one, but may and probably works,, with few if at all minor adjustments), what more can I say, other than it's not the best way to do it but may be useful.

#include <iostream>//needs these three lines
#include <stdlib.h>//needs these three lines
using namespace std;//needs these three lines

int main(int argc, char *argv[])
{
  cout << "A simple Demonstartion of system(\"cls\");" << endl;
  system("PAUSE");	
  system("cls");//clears the screen
  cout << "The first two lines are gone" << endl;
  system("PAUSE");	
  return 0;
}

Dani AI

Generated

Good, compact demo from — calling the shell to clear the screen is simple and works in many quick tests, but it’s brittle and non‑portable. and showed it can be adapted for C and Unix shells, while ’s CodeWarrior failure and ’s trouble with clrscr() highlight two common gaps: (1) IDE output windows often aren’t a real console, so shell commands won’t run the way you expect, and (2) clrscr()/conio.h is non‑standard and not available on all toolchains.

Two dependable alternatives:

  1. ANSI escape sequences — tiny and portable for modern terminals (Linux, macOS, and current Windows terminals that support VT100). They do not require external programs and are safe to use whenever the terminal honors ANSI codes.
#include <iostream>

void clearWithAnsi()
{
    std::cout << "\x1B[2J\x1B[H" << std::flush; // clear screen and move cursor home
}
  1. Windows Console API — use this when you need a reliable clear that works on legacy Windows console hosts (useful if you must support older Windows or need to clear attributes/scrollback properly).
#include <windows.h>

void clearWindowsConsole()
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD written;
    COORD home = {0,0};

    if (!GetConsoleScreenBufferInfo(h, &csbi)) return;
    DWORD cells = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(h, ' ', cells, home, &written);
    FillConsoleOutputAttribute(h, csbi.wAttributes, cells, home, &written);
    SetConsoleCursorPosition(h, home);
}

Quick troubleshooting tips: if a clear call “does nothing,” confirm the program runs in a real terminal (not an IDE pane), check which shell/commands exist on the target OS, and prefer conditional compilation (#ifdef _WIN32) to select the right method. For full-featured terminal handling across platforms, use a library such as ncurses (POSIX) or PDCurses (Windows). Finally, avoid system() when input can be influenced externally — it’s easy to introduce injection risks.

nico 0 Newbie Poster

i modified it a little bit for c and it works , i learned one thing today , thanx

eagleeye 0 Junior Poster in Training

Than it's mission has been fulfilled

bombe 0 Newbie Poster

hmm. wonder if that works for visual c++

bombe 0 Newbie Poster

hmm. wonder if that works for visual c++

bombe 0 Newbie Poster

i just tried it on codewarrior C++ and it didn't work ;/

eagleeye 0 Junior Poster in Training

Sorry about that, but that's the only thing I could find on how to clear the screen, and so far I could get to work only on Dev-C++

1o0oBhP 4 Posting Pro in Training

the code should be ANSI standard code to work on all compilers, and i DONT see any un-ANSI code! it should work on others, i have compiled DevC++ projects on MSVC++ Before without any problems

1o0oBhP 4 Posting Pro in Training

try substituting <cstdlib> in place of <stdlib.h>

dave2point0 0 Newbie Poster

haven't tried this, but it should work on linux if you replace cls with clear

ShawnCplus 456 Code Monkey Team Colleague

system ("cls") and other system commands are handy to get a list of all possible system lines that i know of just go Start>run>cmd>help, it gives a list of all commands that you could use some usefull ones are color ie.
system("color A")
would change the text on the console to bright green (hello matrix)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here are some example how to use windows console api functions

ankit_the_hawk 0 Light Poster

gr88! not able to being use clrscr() was really pissing me off!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.