Clear screen command in C ++

I want to clear the screen after the user enters some numbers in C ++. I am programming in console application mode.

So how do you do it? My OS is win7 and My IDE is CodeBlocks and the compiler is MingW ...

+3


source to share


7 replies


It depends on your OS, If you are using linux:

system("clear");

      

If you are using windows:

system("cls"); 

      



but this makes your application portable, it is advisable to do

cout << string(50, '\n');

      

this line will print lines to make them look like the terminal has been "cleared".

Nice article on this issue: http://www.cplusplus.com/articles/4z18T05o/

+5


source


you can use the clrscr()

one defined in conio.h

. But why don't you ask Google?



ways to clean the shutdown screen

+3


source


you can try system methods. System ("CLS");

+2


source


link conio.h with your compiler. I forgot how to do this. if you will be using a clear screen, repeat this function.

enter code here
void clrscr()
{
  system("cls");
}

      

+1


source


What Microsoft has to say about cleaning up the console:

#include <windows.h>

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   DWORD dwConSize;

   // Get the number of character cells in the current buffer.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole,        // Handle to console screen buffer
                                    (TCHAR) ' ',     // Character to write to the buffer
                                    dwConSize,       // Number of cells to write
                                    coordScreen,     // Coordinates of first cell
                                    &cCharsWritten ))// Receive number of characters written
   {
      return;
   }

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   // Set the buffer attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole,         // Handle to console screen buffer
                                    csbi.wAttributes, // Character attributes to use
                                    dwConSize,        // Number of cells to set attribute
                                    coordScreen,      // Coordinates of first cell
                                    &cCharsWritten )) // Receive number of characters written
   {
      return;
   }

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}

int main()
{
    HANDLE hStdout;

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    cls(hStdout);
    return 0;
}

      

+1


source


system ("CLS");

Brilliant. So what happens if I replace Windows cls with my own malicious keys? You just gave me control, thanks! This is what is called the tailgate and you left it wide open using an unsafe technique.

Source: http://www.daniweb.com/software-development/cpp/threads/76934/how-do-i-clear-my-screen-in-c .

+1


source


One way is to output '\ f' (corresponding to the ASCII form feed character, code 12, which is used by line printers to retrieve the page and is recognized by some common terminals and emulators as a clear screen).

This will not work on Windows.

#ifdef _WIN32
/* windows hack */
#else
std::cout << '\f' std::flush;
#endif

      

0


source







All Articles