Thread: clear screen function probs

  1. #1
    Unregistered
    Guest

    clear screen function probs

    I know there is a section about this in the faq but i'm still having problems. I need a clear screen function in c. The ones i have tried are either not ansi c compatible or like the one which uses the windows.h include crashes the program when it tries to load. So i'm at a loss. Any ideas? i don't want to use the new line method as its for a uni project and it isn't really a way that is going to get me a decent grade. so cheers for any help

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    There's really no good way to clear the screen. If you don't have a gotoxy type function then you will just have to fill the screen with whitespace and work from there or backspace a LOT. If you do have something with gotoxy functionality then try setting the cursor to 0,0 and filling the screen with whitespace, then reset the cursor to 0,0. The screen is cleared.

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

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    9

    Talking


    try this one out,

    char far *ptr=(char far *)0xb0008000;

    main(){

    int i,j;
    for(i=0;i<(80*25/2);i++){
    ++*ptr=' ';
    ptr='0';
    }
    }
    //this will work out you just see whether '0' belongs to black colour or not ,if it is giving different colour replace it with different letter which represents black color

  4. #4
    Unregistered
    Guest
    i'll give that one a go - cheers. - how i miss c++ and the lovely clrscr() - oh well - i'm doing that next semester

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    you dont say what compiler you are using and operating system
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> how i miss c++ and the lovely clrscr()

    clrscr() is not a standard function within C++. It is a non-standard function implemented by certain compiler vendors.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    28
    here's one that works pretty nicely.


    system("cls"); use this if ur running under windows.
    system("clear"); or this if ur running under *nix.

    if u wanna get fancy u can probably write a bit of code that detects w/h OS u're running under and sets a char* to the correct value.

    but so far...this worked pretty nicely.

    system is a function in stdlib.h so u need to include that.
    curiousity killed the cat, but it
    makes for one hell of a programmer.

    Alien.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >system("cls"); use this if ur running under windows.
    This is a brute force method which also happens to be incredibly inefficient. But if you must use it be sure to make it portable, for example:
    Code:
    /* pseudocode */
    #ifdef UNIX
      #define CLRSCR "clear"
    #ifdef WIN
      #define CLRSCR "cls"
    .
    .
    .
    system ( CLRSCR );
    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    28

    question to Prelude.

    hey,

    nice code...i just have one question.. is the variable UNIX and WIN defined no matter which compiler u use?

    that looks pretty slick if it is....

    also, would u need to use LINUX if u're running on a Linux box?
    curiousity killed the cat, but it
    makes for one hell of a programmer.

    Alien.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    no, when you distribute the code, tell the user to add
    Code:
    #define WIN 1
    if they use windows, or:
    Code:
    #define UNIX 1
    if they use *nix.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is the variable UNIX and WIN defined no matter which compiler u use?
    They're environment variables that tell you what system you are on, I believe they are defined as _WIN32, _POSIX_, _MAC, and probably _LINUX though I'm not sure about the last two.

    Though there are a number of ways to go about that, mine was just some pseudocode to get the idea going. Try this and see what it prints. If you get an error on a Linux box then the variable isn't what I thought
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #ifdef _POSIX_ 
      #define PRINTF "POSIX"
    #elif _WIN32 
      #define PRINT "Windows"
    #elif _LINUX_
      #define PRINT "Linux"
    #elif _MAC
      #define PRINT "Mac"
    #endif
    
    int main ( void )
    {
      puts ( PRINT );
      return EXIT_SUCCESS;
    }
    -Prelude
    Last edited by Prelude; 03-07-2002 at 12:37 PM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Clear Screen Function?
    By Punkture in forum C Programming
    Replies: 3
    Last Post: 05-05-2003, 09:25 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM