Thread: FYI - a simple way to clear the screen

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    83

    FYI - a simple way to clear the screen

    okay i always found clrscrn() or whatever it is confusing, and didnt wanna do it everytime i wanted to clear the screen...thanks to the people that helped me in the "running bat files in C " thread...i found an easier way. sorry if you think it was stupid to post this...and that it is only clutter. but i found it useful anyways heres how: (and sorry if most if you knew about this before)
    Code:
    #include <stdio.h>
    #include <system.h>
    
    main()
    {
    	printf("Now you see me..[press any key]");
    	getche();
    	
                    system("cls");
    
    	printf("..now you don't.");
    
    	getche();
    }
    i use the getche at the end because on windows xp it terminates the program automaticaly if hteres no more code...arghhh i hate that.

    so you could use a function with this and call it when ever you need.
    "What this country needs is more free speech worth listening to." - -Hansell B. Duckett

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >system("cls");
    This would work...but only on certain platforms and it would slow down the program considerably because the overhead for the system function is obscene.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Prelude,

    Overhead.

    A concept that I inherently accept - like many others, I would presume - but, what, exactly, are we talking about?

    Conceptually, (where's my thesuarus), I understand that "overhead", at the low-level, brings much into the execution of the program, often inadvertently.

    What exactly is it?

    Where can we look for it?

    How do we avoid it?

    (It's a lot of pressure to be put under, but you're young, and I'm...not quite so young. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    in one of my program system("cls"); was not working.
    so i changed dos textmod then printf("Good day"); and then re sets the dos textmod again. and it works.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    i mean screen becomes clear....

  6. #6
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    this one works with every compiler
    Code:
    void my_cls()
    {
        int i;
        for( i=0; i<=25; i++)
        { printf("\n"); }
    }
    then use my_cls(); to clear the screen by printing 25 new lines

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by dune911
    this one works with every compiler
    Code:
    void my_cls()
    {
        int i;
        for( i=0; i<=25; i++)
        { printf("\n"); }
    }
    then use my_cls(); to clear the screen by printing 25 new lines
    I'm bored so I'm going to be really picky That's 26 newlines, not 25.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    589

    Wink

    Dune911.
    To make your clear screen complete you might want to add how you get the cursor back to the top left corner

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What exactly is it?
    overhead

    1. Resources (in computing usually processing time or storage space) consumed for purposes which are incidental to, but necessary to, the main one. Overheads are usually quantifiable "costs" of some kind.
    In this case the cost of calling system is speed. The system function has to request an operation from environment by sending it an implementation-defined string. As you can probably imagine, this takes quite a bit of time. On my system I called system ( "cls" ); one time and it increased the running time of my program by over 200 milliseconds. Imagine what happens if you tried to clear the screen regularly?

    >Where can we look for it?
    Overhead can be any kind of resource that your program uses, just look for any point where you might use a great deal of it.

    >How do we avoid it?
    Careful design.

    >this one works with every compiler
    Perhaps every compiler, but it may not always work as well as you want it to. What if the size of the console screen is larger than 25 rows? What if there isn't even a screen? A lot of C programs run on platforms where there is no screen output, everything goes to a file, I personally wouldn't want to read a file with 26 newlines in between every bit of real data.

    In the end, there's no standard, portable, effective, efficient way to clear the screen usefully. And in most cases one can do without clearing the screen since those users who would care about it will rarely use a command line program and much of the time those who use command line programs regularly will redirect the output to a file.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    @Hammer
    whoops you're right... i=0 not i=1...

    @Barjor
    can you tell me how, please?

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    I think I would use gotoxy(1,1);

    edit se below....Never needed gotoxy() myself so maybee I should just keep my hands away from the keyboard.

    Also taken from the faq if you are using console

    Code:
    #include <windows.h>
    void gotoxy(int x, int y)
    {
       COORD coord;
       coord.X = x;
       coord.Y = y;
       SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    Last edited by Barjor; 07-17-2002 at 12:33 PM.

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    gotoxy(0,0)?
    i think it wont work.... u might be calling gotoxy(1,1);

  13. #13
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    yes, i am using the console (but never included windows.h, yet)
    thanks Barjor

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Prelude,

    Thanks for the assist on the "overhead" issue. Undoubtedly the most definitive information I've run across on the topic.



    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Ddraw question. Display bitmap on a screen.
    By tegwin in forum Game Programming
    Replies: 0
    Last Post: 05-22-2004, 05:50 PM
  2. Clear Screen Again!!!
    By trang in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 08:36 AM
  3. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 05:14 AM
  4. How do I clear screen in a C program?
    By DarkboyBlue in forum C Programming
    Replies: 6
    Last Post: 08-21-2002, 11:26 AM
  5. clear screen code, can't get it to work
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 01-25-2002, 01:38 PM