views:

166

answers:

4

I want to clear console screen every time the user make an input in C++.

I'm thinking of using system command. For Windows, it is "cls". For Linux, it is "clear". Is there a way check which system to use the appropriate command in c++?

Thanks.

+1  A: 

You could use C Preprocessor - Conditional Syntax in C++ too.

#ifdef linux
    //clear
#else
    //cls
#endif
S.Mark
+3  A: 

No, there isn't a C++ Standards way to do it.

You can instruct your windows compiler to define the WINDOWS macro and your linux compiler to define the LINUX macro though; some compilers do this by default (for example Visual Studio defines WIN32).

Andreas Bonini
+1  A: 

There is a trick solution:

if (system("clear"))
  system("cls");

For Unix it just working properly. For Windows, it will prompt an error like

'clear' is not recognized as an internal or external command, operable program or batch file.

While it will be cleared right now by "cls". So in the Command Prompt Window, it will not leave any track. :D

RouMao
neat trick ;)..
Inverse