Teen Programmers Unite  


Return to forum top

Yet another c++ question

Posted by jay_dee [send private reply] at May 28, 2002, 06:37:32 PM

this is probably a stupid question....and people are going to laugh at me for the rest of my life...but ohwelllll....

is there a way to clear the screan in c++?

Posted by jay_dee [send private reply] at May 28, 2002, 06:39:04 PM

i just had a idea..i could just make a loop of
cout << "\n"; but theres got to be a better way..hmmm

Posted by AngelOD [send private reply] at May 28, 2002, 08:52:20 PM

I'm not entirely sure, but try looking up the function clrscr(). Can't remember what headerfile it's in, but something tells me to look at conio first.. I think it was mentioned before in another thread, but I can't remember which one. :)

Posted by CodeRed [send private reply] at May 28, 2002, 08:55:10 PM

In the header file conio.h is the function angelOD was talking about, clrscr(), it only works in console DOS (obviously, since it is in conio)

Posted by jay_dee [send private reply] at May 28, 2002, 10:01:07 PM

yeah, I looked in past threads before posting, but didn't see anything, maybe I'm just blind.
Anyways, How do I do it in Linux?


Posted by buzgub [send private reply] at May 29, 2002, 01:06:36 AM

you could do something like

or use something like ncurses.
Posted by unknown_lamer [send private reply] at May 29, 2002, 08:41:30 AM

Ncurses makes it easy, or you could use termios (POSIX only). Ncurses is probably the best solution because it is portable (yes, there is an implementation of curses for Windows/DOS). Of course, then curses will control your screen and you have to use curses for everything (or bad things (tm) happen). This should also work:

#include <cstdio>
std::printf("\033c"); /* reset terminal (optional?) */
std::printf("\033[2J"); /* clear screen */

That uses some vt100 escape codes...I have no idea if that would work on Windows (someone removed cygwin on this machine...grrr!) but it is worth a try. You can also use iostreams:

#include <iostream>
std::cout << "\033c" << "\033[2J";

Posted by buzgub [send private reply] at May 29, 2002, 09:12:00 PM

Presumably it would be possible to init ncurses, use it to clear the screen, then tell it to go away.

Posted by unknown_lamer [send private reply] at May 30, 2002, 08:56:35 AM

Not always. Ncurses makes a backup of what the screen looks like and restores it when you exit...(at least it does this for everything I have written). It is really wierd though--it restores the screen in aterm, but not on the normal console or xterm. So I guess curses has different behaviour depending on your terminal. The vt100 escape codes are the easiest way anyway (I'm at school on the Windows machine now, I'm grabbing emacs and will see if they work in a minute). ... yep, this program clears the screen:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
int i;
for (i=0;i<15;++i)
printf ("MOO MOO MOO!");

printf ("Sleeping for 5 seconds and sleeping\n");
sleep (5);
printf("\033c"); /* reset terminal (optional?) */
printf("\033[2J"); /* clear screen */
printf ("Was the screen cleared?\n");
return 0;

You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.