How to clean the screen in C ++?

0

How to clean the screen in C ++? I have already put the library: ** # include < stdlib.h> ** and the code: system ("cls") , but gives the error: " sh: 1: cls: not found ". How do I resolve this?

    
asked by anonymous 13.04.2018 / 00:11

2 answers

2

Deleting the screen is an operation dependent on the environment in which the program operates. For example, a graphical program does not even have this operation because it is not running on a terminal.

That said, it looks like you are running the program in a UNIX / Linux environment, so I suggest replacing "cls" with "clear" because "cls" is the MS-DOS / screen.

    
13.04.2018 / 00:16
0

As I said, it depends on the environment, however, as suggested, I'm already afraid of the command:

clear

That is usually used on unix-like systems (linux for example) and the command:

cls

Used on Windows systems.

If the error occurs it may be because it is running cls on a Linux (or similar) system.

To create a 'certain compatibility' between different systems can be a bit complex, there are those who suggest using compiler definitions, for example (it's a simple example):

#if defined(_WIN32) || defined(_WIN64)
    system("cls");
#else defined(__linux__) || defined(__unix__)
    system("clear");
#endif

But this of course may vary greatly for each compiler, that is, the compilers or libs available may use other definitions to identify the system that was compiled, so one suggestion that I believe would be more certain would be to use the || and with this pass both commands ( cls and clear ), for example:

system("clear||cls");

The above command will run like this, first it tries to execute clear , if it is a command available on the user's system then it will run, otherwise it will try to execute cls .

This can make your program a bit more "portable" between different systems without worrying so much about different types of compilers.

    
14.04.2018 / 16:36