5

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

so how to do it? My OS is win7 and My IDE is CodeBlocks and the Compiler is MingW...

2
  • 1
    what about clrscr(); method???? Commented Mar 7, 2012 at 8:15
  • 1
    include your os and IDE in the question because the answer depends on both Commented Mar 7, 2012 at 8:21

7 Answers 7

8

It depends of your OS, If you use linux:

system("clear");

If you use windows:

system("cls"); 

but this make your application lees portable, it's preferable to do

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

this line will print lines to seem like the terminal was 'cleared'.

A good article about that problem: http://www.cplusplus.com/articles/4z18T05o/

1
3

You can use the clrscr() defined in conio.h.

Ways to clear screen the output screen.

3
  • For using Clrscr which header I should use?
    – Inside Man
    Commented Mar 7, 2012 at 8:18
  • I google before asking, and come here. Google doesn't create content, it helps you find content by indexing it
    – Ast Derek
    Commented Mar 15, 2012 at 7:07
  • Oh did you? and you didnt fnd any relevant answers? Try checking this google.co.in/… Commented Mar 15, 2012 at 7:13
2

you can try system methods E.g. system("CLS");

1

link the conio.h in your compiler. I forgot how to do that. if you will use clear screen repeatedly put this function.

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

That's what Microsoft has to say about clearing a 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's 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

system("cls");

Brilliant. So what happens if I replace the Windows cls with my own malicious cls? You've just given me control, thanks! This is what's called a back door, and you left it wide open by using an insecure technique.

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

0

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

That won't work on Windows.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.