Thread: clearing the screen

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    clearing the screen

    Is there a way to clear screen after a while loop has taken place, so it starts program again but not at the bottom of the last text?

    Brad.

  2. #2
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    #include <conio.h>

    after declaration, with this command clrscr();
    - - fUnKy F3m@le - -

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You can use clrscr() like hermit suggested but I think this command is not supported on all systems. If it's not supported on your system you can always use the system command:
    Code:
    system("cls");

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Monster
    If it's not supported on your system you can always use the system command:
    Code:
    system("cls");
    And cls is supported on all systems is it? [/sarcasm]
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    i really like option 6! throw your monitor outta of the window!
    ROLF
    - - fUnKy F3m@le - -

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And cls is supported on all systems is it?
    Technically, you could use conditional compilation to fix that. It's still pretty nasty, but portable:
    Code:
    #if SYSTEM == MSDOS
      #define CLRSCR system("cls");
    #elif SYSTEM == POSIX
      #define CLRSCR system("clear");
    /* etc... */
    -Prelude
    My best code is written with the delete key.

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Hammer

    And cls is supported on all systems is it? [/sarcasm]
    You're not listening... I said you could always use the system command.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Monster


    You're not listening... I said you could always use the system command.
    ... and the example you gave was specifically using cls, with no mention that that was OS dependant. A newbie might not spot that and could get confused when it didn't work.

    And yes I was listening
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Hammer


    ... and the example you gave was specifically using cls, with no mention that that was OS dependant. A newbie might not spot that and could get confused when it didn't work.

    And yes I was listening
    A newbie should always read the manual pages. In the manual pages you can read how the function works...

    But since manual pages are like FAQ pages (nobody reads it), you are probably right. I should have told that the cls command is an operating system command and can be different on any OS.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Option 1

    #include <conio.h>
    ... code ...
    clrscr();
    ... more code...

    advantages:
    clears the screen...

    disadvantages:
    ... but only on MS-DOS systems with compilers that support it in this manner.
    Who says there is a screen connected to standard output anyway?

    Errors in FAQ:
    nothing significant, except for not mentioning there might not be a screen.

    Option 2

    putcharing newlines and scrolling over

    Advantages:
    none

    disadvantages:
    there may not be a scree - printing 50 lines to the printer doesn't clear the screen
    printing 50 lines to a 63 line screen also doesn't clear the screen

    Errors in FAQ:
    "It works" is wrong. It clearly doesn't work quite so widely as is implied by this phrase.

    Option 3

    Using a library to do it for you

    Advantages:
    You don't have to think about whether it works or not
    Possibility of slightly portable code, but not universally portable code.

    Disadvantages:
    Getting a library to do the job
    Getting a library which doesn't fall into the 'clearing the screen' errors
    Who says there is a screen anyway?

    Errors in FAQ:
    Nothing significant for this bit, except for the possibility that there might not be a screen attached to stdout.

    Option 4

    Write directly to video memory

    Advantages:
    None

    Disadvantages:
    Highly platform dependent
    Who says the program is outputting to that display device, to that framebuffer or even outputting anything to the screen at all?

    Errors in FAQ:
    Again, this certainly doesn't 'work' since it's completely non portable and depends heavily on the machine running it.

    Option 5

    Using curses on unix systems

    Advantages
    It works....

    Disadvantages
    ... but only on unix systems which have curses.

    Errors in FAQ:
    None, but this is really a special case of option 3

    My prefered answer is simply:
    There is no way of doing it in C. No matter what you will need to use platform specific techniques. Find the techniques for your platform and use them, being aware that moving your code to other platforms (or even future or past versions of the same platform) might mean your code doesn't work.

    Ian Woods

  12. #12
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb

    Option 4

    Write directly to video memory

    Advantages:
    None

    Disadvantages:
    Highly platform dependent
    Who says the program is outputting to that display device, to that framebuffer or even outputting anything to the screen at all?
    This should work w/o problems w/ the following conditions...
    1. Platform is DOS.
    2. Screen Resolution (text mode) 25×80 (25 rows by 80 cols)

    #include<stdio.h>
    main()
    {
    char far *vm= (char far *) 0xB8000000;
    int i;
    for(i=3998;i>=0;i-=2)
    *(vm+i)=32; /* space character */
    }

    With little modifications u can run it for other text modes also.

    -Harsha

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Originally posted by vsriharsha


    This should work w/o problems w/ the following conditions...
    1. Platform is DOS.
    2. Screen Resolution (text mode) 25×80 (25 rows by 80 cols)

    ... code ...

    With little modifications u can run it for other text modes also.

    -Harsha
    Note of course that if the computer is in /any/ graphics mode or any other text mode this code won't work. It also doesn't work if I have specified a different page to be shown than the first one, and as you've specified, currently only works for a single text mode.

    The whole point of what I've said is that any technique for clearing the screen is non-portable and is often specific on many assumptions about the system you're using. You've coded a technique which suffers from the problems I stated for that option (writing directly to screen memory).

    The cleanest way is to use the functions provided by your operating system/compiler vendor or to use a portable library like curses which (hopefully) will do things properly in a system specific manner.

    Ian Woods
    Last edited by hairyian; 05-02-2002 at 12:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  3. clearing the screen
    By ssharish in forum C Programming
    Replies: 2
    Last Post: 02-01-2005, 09:00 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. Clearing the screen
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 05-23-2002, 01:40 PM