how to clear the stdout ?

Discussion in 'C++' started by Joseph, Sep 16, 2004.

  1. Joseph

    Joseph Guest

    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!!!
     
    Joseph, Sep 16, 2004
    #1
    1. Advertising

  2. Joseph

    David Hilsee Guest

    "Joseph" <> wrote in message
    news:Xns9566AE53A1569wutongjoe@203.96.216.21...
    > 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;
    }

    --
    David Hilsee
     
    David Hilsee, Sep 16, 2004
    #2
    1. Advertising

  3. The most portable way is:
    system("cls");

    HTH,
    Gernot
     
    Gernot Frisch, Sep 16, 2004
    #3
  4. Joseph

    Materialised Guest

    Gernot Frisch wrote:
    > The most portable way is:
    > system("cls");
    >
    > HTH,
    > Gernot
    >
    >

    bash: cls: command not found
    --
    perl -e 'printf "%silto%c%sal%c%s%ccodegurus%corg%c", "ma", 58, "mw",
    107, 'er', 64, 46, 10;'
     
    Materialised, Sep 16, 2004
    #4
  5. Joseph wrote:
    > ...
    > 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.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Sep 16, 2004
    #5
  6. Joseph <> writes:

    > 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

    --
    | Nicolas Pavlidis | Elvis Presly: |\ |__ |
    | Student of SE & KM | "Into the goto" | \|__| |
    | | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|
     
    Nicolas Pavlidis, Sep 16, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elad
    Replies:
    0
    Views:
    437
  2. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,129
    Mark Fitzpatrick
    Jan 31, 2008
  3. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    1,077
    Steven Cheng[MSFT]
    Mar 6, 2007
  4. Andreas S
    Replies:
    3
    Views:
    289
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    384
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page