how to clear the stdout ?

J

Joseph

Hi all,

I am doing my assignment and have a question ,how can I clear the standard
output(screen)?

following is the fake code:

=====================================

main(){


for loop(int i++){

printf (" %d",i);

}

}
====================================

the problem is,I want to clean the previous output number and then print
only the new number .So it is like a on-screen counter somehow.




Thanks a lot guys!!!
 
D

David Hilsee

Joseph said:
Hi all,

I am doing my assignment and have a question ,how can I clear the standard
output(screen)?

following is the fake code:

=====================================

main(){


for loop(int i++){

printf (" %d",i);

}

}
====================================

the problem is,I want to clean the previous output number and then print
only the new number .So it is like a on-screen counter somehow.

This sounds pretty similar to a FAQ. See the FAQ
(http://www.parashift.com/c++-faq-lite/), section 15 ("Input/output via
<iostream> and <cstdio>"), question 20 ("How can I clear the screen? Is
there something like clrscr()?").

There are some (non-portable) ways to do what you want. For instance, on
Windows, using VS.NET2003, the following code displays a counter that is
periodically overwritten:

#include <stdio.h>

int main() {
int i = 10;
printf("Counter: %d", i);
for ( int i = 11; i < 20; ++i ) {
// Use a "hack" that uses backspace to erase the number
printf( "\b\b" );
printf( "%d", i );

// Do something to pause here, like Sleep()
for (int j = 0; j < 100000000; ++j ){}
}
printf("\n");
return 0;
}
 
A

Andrey Tarasevich

Joseph said:
...
I am doing my assignment and have a question ,how can I clear the standard
output(screen)?

following is the fake code:

=====================================

main(){


for loop(int i++){

printf (" %d",i);

}

}
====================================

the problem is,I want to clean the previous output number and then print
only the new number .So it is like a on-screen counter somehow.
...

Instead of clearing the entire screen you might want to try to print
every new number at the same place where the previous one was printed
(thus overwriting the previous one) by doing it like this

printf("%d\r", i);

I don't think it is 100% portable in theory, but in practice this will
work on many platforms.
 
N

Nicolas Pavlidis

Joseph said:
Hi all,

I am doing my assignment and have a question ,how can I clear the standard
output(screen)?

following is the fake code:

[...]

the problem is,I want to clean the previous output number and then print
only the new number .So it is like a on-screen counter somehow.

One way is to print out a lot of blank lines, so the old output will
disapaire, another way is using the preprozessor:
//do this in a seperate header file
#if defined LINUX
#define CLEAR_SCREEN clear
#elif defined WIN32
#define CLEAR_SCREEN cls
//for more platforms
#endif

or just spliting it into a front and a back end, by using in the backend
the platform specfic functions or libraries.

On Linux for example ncurouses and on windows for example the code that
David posted.

HTH && kind regards,
Nicolas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top