Thread: Clearing Text...

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Clearing Text...

    If I have a program with text and I want the user to press a key to clear the text, how do I do it? Say this is my code:

    Code:
    #include <iostream>
    int main()
    {
      cout<<"....."<<endl; 
      cout<<"Press c to clear the text..."
      return 0;
    }                        //Note: Not adding chars or neccesary things for
                              //this program...
    How do I make c clear the text? (Yes, I know I need c as a char...)
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Have you considered all the possibililites that reading the FAQ has to offer?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You have to overwrite it with spaces. i.e ' ' or its ASCII value of 32.

    This can be tricky, because there is no standard C++ way to move the cursor on the screen. Maybe the gotoxy() FAQ will help a bit.

    If you know the width of the console display (if it's not a re-sizable window) you can make string of spaces, and overwrite the whole line with spaces.

  4. #4
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    If all you're writing to screen is "....." then you may as well use the clrscr() command ( conio.c ) to clear the whole console screen, or using system("cls").

    Code:
    #include <iostream>
    #include <conio.c>
    int main()
    {
        char esc;
        while(esc != 'c')
        {
            cout<<"....."<<endl; 
            cout<<"Press c to clear the text..."
        }
        clrscr();
        char exit = getch(); // this allows the console to remain open
                                          // until a key is pressed, same as system(pause);
        return 0;
    }                        //Note: Not adding chars or neccesary things for
                              //this program...
    Last edited by Korhedron; 11-25-2003 at 02:21 PM.

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Angry

    Using that:

    Code:
    clrscr();
    Will give me the error: [Linker error] undefined reference to `clrscr' and I don't like that error...I can't figure out how to fix it...

    Code:
    #include <iostream>
    #include <conio.h>
    int main(int argc,char *argv[])
    {
      char start;
      while(start!='A')
      {
        cout<<"CORN ON THE COB!"<<endl; // :D
        cout<<"Press A to clear the text...";
      }
      clrscr();
      return 0;
    }
    An example of my code, the one I get an error with.
    (Note: I didn't add my real code, and I skipped things [like to quit] to make it easier on your eyes...)
    Last edited by SirCrono6; 11-25-2003 at 04:10 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    If you are using Windows, just do:
    Code:
    system("cls");
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It's usually best to avoid clearing the screen whenever you can. There are a few good reasons for it:

    1) Not portable: There's no way to make such an operation portable. So if you really need it, your compiler will probably support an extension that allows you to clear the screen, or functions that can be used to write code that clears the screen. Because of this, we can't help unless you tell us what compiler and operating system you use.

    2) Flame bait: Clearing the screen is a long hated question on these forums. If you ask it, you'll most likely get at least one snide remark. Avoiding this question saves you losing l33tness points with the regulars.

    3) Slow: The typical suggestion for a Windows-portable screen clear is
    Code:
    system ( "CLS" );
    This is a blatant abuse of the system function, which should only be used rarely and for truly exceptional situations because it is dreadfully inefficient.

    4) Ugly: Most often when something like clrscr() is supported and used, calls to it are littered throughout the code, thus making compilation by those of us without compilers that support the solution impossible without heavy modification. I personally despise making lots of trivial little changes just to get code to compile so I can help with an unrelated problem.

    >while(start!='A')
    start has an indeterminate value at this point, merely accessing it is undefined behavior. Since you will be seeing that term often around here (mostly from me ) it means that bad things can happen. Avoid undefined behavior.

    >cout<<"CORN ON THE COB!"<<endl;
    Your code will not compile. Undecorated use of cout and endl without some form of using declaration is illegal.
    My best code is written with the delete key.

  8. #8
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Unhappy

    Nothing is working...(still). Errors, errors, and more errors...That's all my compiler can say...

    Edit: Oops! Haven't read Prelude's!

    Compiler: Dev-C++
    Operating System: Windows ME

    Decorating cout? What's that supposed to mean?


    Undefined behavior?
    Last edited by SirCrono6; 11-25-2003 at 05:01 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Errors, errors, and more errors...
    Welcome to the world of programming. Get used to seeing plenty of errors on a regular basis. Post your code and we'll help you with them.
    My best code is written with the delete key.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the first error you should run into is an error about cout being undeclared, like prelude said... try this:
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;  //not the best way, but good enough for beginners
    
    int main(int argc,char *argv[])
    {
      char start;
      while(start!='A')
      {
        cout<<"CORN ON THE COB!"<<endl; // 
        cout<<"Press A to clear the text...";
      }
      clrscr();
      return 0;
    }
    and to answer your question about a good way to clear the screen... as far as i know, there is no good way to clear the screen, and clrscr() isn't supported by all compilers... it looks like your compiler doesn't support it

    I have a question about the clrscr() function... why does conio.h have a prototype for it if it's never defined... a short clip from conio.h:
    Code:
    #include <stdio.h>
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    #define BLINK 0
    
    typedef enum
    {
        BLACK,
        BLUE,
        GREEN,
        CYAN,
        RED,
        MAGENTA,
        BROWN,
        LIGHTGRAY,
        DARKGRAY,
        LIGHTBLUE,
        LIGHTGREEN,
        LIGHTCYAN,
        LIGHTRED,
        LIGHTMAGENTA,
        YELLOW,
        WHITE
    } COLORS;
    
    
    #define cgets	_cgets
    #define cprintf	_cprintf
    #define cputs	_cputs
    #define cscanf	_cscanf
    #define ScreenClear clrscr
    
    /* blinkvideo */
    
    void clreol (void);
    void clrscr (void);
    
    int _conio_gettext (int left, int top, int right, int bottom,
                        char *str);
    /* _conio_kbhit */
    
    void delline (void);
    
    /* gettextinfo */
    void gotoxy(int x, int y);
    /*
    the compiler still complains about it not being there... is there something about headers i'm missing here?
    Last edited by major_small; 11-25-2003 at 06:26 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Unhappy ...

    You do know, you posted my exact code (no changes)! Well, I'm only going to clear the screen once. Is it bad even if I use it just once? Just one clear screen and I'll never use it again. All I need is a working thing to do it...


    Edit: Oh, wait! I see, I forgot about

    Code:
    using namespace std;
    But, anyways, in my actual code it is that way...
    Otherwise, exept for different cout statements, they are the same. Also, I haven't added in the

    Code:
    clrscr();
    either...
    Last edited by SirCrono6; 11-25-2003 at 06:39 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well... here's a breakdown

    your code
    +using namespace std;
    -clrscr();
    =most likely infinite loop...

    try something like this (for now)
    Code:
    #include <iostream>
    
    using std::cin;  //this was the better way i was talking about
    using std::cout;
    using std::endl;
    
    int main(int argc,char *argv[])  //do you know what this does or is it prewritten code?
    {
      char start='B';  //you should almost always start a loop with a known variable
    
      while(start!='A')  //I would have used a do while loop
      {
        cout<<"CORN ON THE COB!"<<endl;
        cout<<"Press \'A\' to clear the text..."<<endl;
        // ^ write endl so when the user enters something other than 'A', it will look nice
        //another note: in this case entering an 'A' will exit the program
        cin>>start;  //infinite loop==bad
      }
      return 0;
    }
    that should work... i think... but it's not very useful for anything more than a looping tutorial...
    Last edited by major_small; 11-25-2003 at 07:00 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    This is why I moved to windows programming. Consoles are just too incredibly dodgy. Sure, they're good when you're learning C++, but once you know enough, there's really no reason to stay. It's ugly, can't display graphics, can't display much text even, and has all these portability issues.

    *starts flame war and then retreats*
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by bennyandthejets
    *starts flame war and then retreats*
    I hate people like you
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Code:
    for(int i = 0; i < 36; i++) {
     cout << endl;
    }
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My text doesn't display
    By joeprogrammer in forum Game Programming
    Replies: 11
    Last Post: 02-23-2006, 10:01 PM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  3. Text positioning and Text scrolling
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2004, 12:35 AM
  4. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 04:33 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM