Thread: Clearing Text...

  1. #16
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    dont use :
    Code:
    system ("CLS");
    people can replace cls on your system with, say, a virus or the fdisk command or sommat....... its a security issue. Read up on the Windows console API, or go look at the FAQ.... Prelude wrote it, so......

    *all praise almighty, prelude, who provides answers to all *

    EDIT : btw, get very used to compile errors.... and thank your compiler development team when you see them. Imagine debugging without them! Besides, you have yet to have some fun with runtime errors...... fun having your computer crash because you overstepped the end of your vector, or forgot to initialise you variables......
    Last edited by littleweseth; 11-25-2003 at 08:59 PM.
    Ph33r the sphericalCUBE

  2. #17
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Hahahah, my way is safest .
    Do not make direct eye contact with me.

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

    Talking Downsides!

    Okay guys, I've heard too many downsides to clear screen commands (I don't need to know anymore!) I've also decided not to use the command anymore. I'm not going to clear the screen, I'm just going to keep on going ( )! But seriously:

    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    int main(int argc,char *argv[])
    {
      // Okay, now what should I put here?
      // and here...
      {
         cout<<"Bla bla bla..."<<endl;
         cout<<"Press A to begin"<<endl; //I want A to trigger another 
         // cout statment...
      }
      // I'm expecting something here,
      // and here (but I don't know...)
      {
         cout<<"Bla bla bla..."
         //more program...
      }
      return 0;
    }
    So, what I need after int main(), and how to make A trigger a cout statement. I left those two lines because I'm expecting them to make A trigger the cout statment.
    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

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

    Talking Scratch that!

    You guys can still tell me how to have A trigger the cout statement, and I'll use it if you do. But I'm just going on without that, It's not really needed...
    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

  5. #20
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    but lurker, how do you know that there will be 36 or less lines in the console? And how do you repo the cursor afterwards, if you use C++ ( no gotoxy() )?

    Did you want 'enterless' 'A' pressing (ie no enter needed before the program evaluates?

    Code:
    #include "conio.c"  //Getch()
    #include <ctype.h> //For the toupper thing
    
    char Input = 0;
    Input = getch();
    fflush (stdin) //prevent the input echoing twice
    Input = toupper (Input) //Capitalise the input
    if ( Input == 'A' )
        { do somethingorother }
    less complicated.....

    Code:
    char Input = 0
    cin>>Input;
    if ( Input == 'A' || Input == 'a' )
        { Do somethingorither here }
    Ph33r the sphericalCUBE

  6. #21
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    if you still want to clear the screen, here's preludes way of doing it ( uses windows console API, so not portable, but works for any size screen)

    Code:
    #include <windows.h>
    int ClearScreen()
    {
    	COORD                       coordScreen = { 0, 0 };
    	DWORD                       cCharsWritten;
    	CONSOLE_SCREEN_BUFFER_INFO  csbi;
    	DWORD                       dwConSize;
    	HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	GetConsoleScreenBufferInfo(hConsole, &csbi);
    	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    	FillConsoleOutputCharacter(hConsole, TEXT(' '),
    	                            dwConSize, coordScreen, &cCharsWritten);
      GetConsoleScreenBufferInfo(hConsole, &csbi);
      FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
                                  dwConSize, coordScreen, &cCharsWritten);
      SetConsoleCursorPosition(hConsole, coordScreen);
      return 0;
    }
    Ph33r the sphericalCUBE

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how do you know that there will be 36 or less lines in the console?
    How do you know that there is even a console? The standard doesn't specify that there needs to be one.

    >fflush (stdin) //prevent the input echoing twice
    Ack! Using fflush on input streams results in undefined behavior, please don't use it.

    >and how to make A trigger a cout statement
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
      char ch;
    
      cout<<"Continue by typing A: "<<flush;
      while ( cin.get ( ch ) && toupper ( ch ) != 'A' ) {
        if ( !cin.good() )
          cin.clear(); // Clear the stream of errors
        // Discard extraneous data
        cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
        cout<<"Please type A: "<<flush;
      }
      cout<<"You typed A!"<<endl;
    }
    The use of cin.clear and cin.ignore are for when cin.get fails, and for removing extra leftover characters (like a newline).

    >if you still want to clear the screen, here's preludes way of doing it
    Um...I didn't write all of the FAQ, just a few large portions (and a few huge portions if Hammer decides to add some of my other works). That particular function was written by lightatdawn I believe, though I could be wrong on that point.
    Last edited by Prelude; 11-26-2003 at 08:52 AM.
    My best code is written with the delete key.

  8. #23
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    okay, so maybe i shouldn;t type forum posts when i start seeing sheep crying *zzzzZZZZZ!* either. Same goes for porgramming kids.

    undefined behaviour? please define undefined behaviour (sound like a paradox when i type that last bit, so....)
    Ph33r the sphericalCUBE

  9. #24
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >please define undefined behaviour
    The standard definition is
    behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this
    International Standard imposes no requirements. Undefined behavior may also be expected when this
    International Standard omits the description of any explicit definition of behavior. [Note: permissible undefined
    behavior ranges from ignoring the situation completely with unpredictable results, to behaving during
    translation or program execution in a documented manner characteristic of the environment (with or without
    the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a
    diagnostic message).
    Put simply, anything at all can happen, from your program running properly to launching thermonuclear warheads and throwing the planet into WWIII (if you have the correct hardware options, of course).
    My best code is written with the delete key.

  10. #25
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    If you truly need to get the screen blank, there's one fully portable solution that also saves some power. Find the button and turn off the monitor!

  11. #26
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Find the button and turn off the monitor!
    That still isn't portable. Finding the button at best would result in an infinite loop if there is no monitor. Remember that the standard doesn't require one.
    My best code is written with the delete key.

  12. #27
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    you and your technicalities! I guess that's why you know more than the rest of us....

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