Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I clear the screen?

Status
Not open for further replies.

RSTR

Programmer
Mar 25, 2000
27
US
What is the command that clears the screen?


rstr@spacemail.com

 
#include <stdlib.h>

int main(/* ... */){


/*
. Whatever
. Your
. Code
. Does
*/

system(&quot;clear&quot;); //Same as typing clear at a prompt
// See man system for more info on return value.

/*
. More
.Stuff here
.
.*/

int retval;

return retval;
}
 
Thanks. That worked.

But now I have another problem, it doesn't work the way I
Expected it to.

I am trying to make a clock. I got the numbers and the digits to work, but the system(&quot;cls&quot;) statement refreshes faster than the code posts the numbers, so instead I only see little bits and pieces of numbers once in a while.

Is there a way that the numbers will refresh over the top of the previous post, so that it will look like the clock is changing numbers in the same place?

did I explain that right?


rstr@spacemail.com

 
if you won't use curses or slang for a better formatting of output, you can go by \n and \t to the place you want to fprintf the numbers and sleep( 1 ) between each call of showing the time.

noka
 
Sounds good to me, I'd do this:

1. Write the time
2. Pause for 1 sec.
3. Clear the screen
4. repeat

This is the quick and dirty way, though, but I don't know what curses or slang are, so you may want to look into that. Maybe noka will post some more info about where to read up on it...

Mike B.
 
sorry folks,
thought everybody would know all about curses and slang.
try the excellent 'Writing Programs with NCURSES'
by Eric S. Raymond and Zeyd M. Ben-Halim for a start with curses.
S-lang is a C-like programming language, designed to be embedded in programs. It provides standard screen handling functions, similar to curses.
if you like curses then try the curses development kid written by Mike Glover.
i know everybody wants a gui like M$-Windows, but this is a c++: unix forum and the customers i'm programming for use terminal i/o. ;-)
noka

 
Thanks for your input



rstr@spacemail.com

 
Is there no other way to clear the screen other then the
Code:
system()
call?
 
Don't know, try the post about curses, is there a specifc problem you ahve with system(), if so, post it.

Mike B.
 
hi,

first of all i think u just need to fflush your output buffers (stdout or cout) that would take care of the no. being o/p (visibly) to screen. Also call the clear b4 u print and not after u print in ur processing loop. i don't think giving a pause is the correct way to go about it.

secondly using system calls like &quot;clear&quot; is depending too much on the system settings( think @ someone replacing `which clear` by a shell script to copy bash and then call clear.orig!! u would never know of this hack!!)... hence, u wouldn't want to do so if u r running root or suid bit set programs.. (if u care) so a better way is to look in to the os specific tty header files and use that function appropriately.. (start with `man -a stty` on unix)
u can ofcourse avoid all this pain by using ncurses library !!

hth,

shail
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top