Thread: clearing screens

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    13

    Unhappy clearing screens

    I am really new to C so have extremely limited knowledge at the moment. I am using a menu function in my program eg. if the user selects option 1 then it goes to that particular function, but I was wondering how you would clear the screen so the function appears at the top of the screen, just like when you first execute your program?

    Thanks in advance!

    Speve!

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Check the FAQ.
    zen

  3. #3
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    new users should check the faq list first an dthe faq board, also list which environment you are programming in and which o/s you program for.

    but check out clrscr()
    or system calls (slow, but can work)
    Monday - what a way to spend a seventh of your life

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    There is no way in ANSI C to clear the screen, because it would be horrible for ANSI to assume that the computer you are compiling C for even has a screen. Just think about it... how many mainframes do you know of with a screen? Anyways you have to find out the way that your compiler/system clears the screen which is different for almost every system that has a screen...
    if all else fails get the screen size and output that many space characters (@ least that way it is portable)
    The better way to do it though would be to use an abstraction layer. Write a function, lets say, foo() that clears the screen. Place it in a file called clrscr.c, and in the function have it call the correct function for that compiler and system and include that .c file in the project. Then you can call foo() to clear the screen, and make multiple clrscr.c files to go with your different compilers and systems. Also make sure you either make a clrscr.h file that includes a prototype for foo() or write a prototype for it in your program.

    one fish two fish
    red fish blue fish

  5. #5
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Red face there's a way



    hey... you could solve this problem by entering this system code. " system ("cls"); "

    if you really know more about dos command, then i/m sure you could expert using system code. more example,

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    As long as the program is a 16 bit dos app this will work.

    void cls();

    void cls(void)
    {
    _asm
    {
    mov ah,02
    mov bh,0
    mov dx,0
    int 10h

    mov ah,9
    mov cx,2000
    mov bl,07h
    mov al,' '
    int 10h
    }

    return;
    }

    How it's called in your main

    cls();

    Try it in a win32 console app... just don't expect it to work.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    beely while system("cls") may work in dos or windows, linux uses the command clear, so cls will not work in linux. Also system takes a very long time to call, and it would be significantly faster to output space characters to the screen, though it starts text at the bottom of the screen instead of the top like more traditional clear functions...
    one fish two fish
    red fish blue fish

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Lightbulb clear screen

    Jeeeeeezzzz, I dont know why you guys go forth and make it so bloody complicated. Theres an easy function for all you Noob programmers out there. Its called clrscr(); WOW!!! Just make sure you have included conio.h as a library and ur set :-)

    So just remember...
    #include <conio.h>

    and the function
    clrscr();

    and you'll be sweet

  9. #9
    Registered User bobthefish3's Avatar
    Join Date
    Oct 2001
    Posts
    22

    Lightbulb clrscr() is compiler-specific

    just to let you know, if you try and use the clrscr() function out of the conio.h header file, it may not work, because that is a compiler-specific function and is not ANSI, i know it works on a borland compiler, but it does not exsist on the microsoft compilers (unless you your self right the function -- which i would not attempt myself if i were you).
    -please do not mess with the incredible radioactive super-fish... thank you.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    197

    There is an ANSI solution!

    Hi!

    You should only know your screen settings for it:

    Code:
    int count;
    for(count=0;count<20;count++, putchar('')) ; /* e.g for 80*20 a screen */
    klausi
    Last edited by klausi; 12-30-2001 at 08:27 AM.
    When I close my eyes nobody can see me...

  11. #11
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158

    Talking

    About the conio.h header file, I heard that it is not supported in Linux system. This means that you won't be able to use the clrscr() function with the conio.h file if you are compiling under Linux...
    It's unfulfilled dreams that keep you alive.

    //netboy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Clearing Terminal
    By mrginger in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 11:58 AM
  2. Clearing unexpected values out of cin
    By IdioticCreation in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2006, 07:27 PM
  3. clearing cstring
    By curlious in forum C Programming
    Replies: 4
    Last Post: 11-30-2004, 10:30 AM
  4. Screens of Projects
    By Perspective in forum Game Programming
    Replies: 29
    Last Post: 08-20-2003, 07:05 PM
  5. Clearing the screen
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 05-23-2002, 01:40 PM