What is a simple alternative to the system ("CLS")?

After reading why system()

is evil, I decided not to use features like system("CLS")

and system("PAUSE")

. Are there any simple OS independent alternatives?

+3


source to share


4 answers


There is no standard C ++ 11 alternative for Windows system("CLS")

as C ++ 11 is not aware of screens. However, consider using GNU readline or ncurses (both work on Linux and have Windows gnuwin32 flavors ). See also POCO or Qt



0


source


There are two ways:

Function creation:

void ClearScreen()
{
    int n;
    for (n = 0; n < 10; n++)
        printf( "\n\n\n\n\n\n\n\n\n\n" );
}

      



This just creates a function that displays 100 new lines. Slow, pathetic, but it works.

Also the only other OS-dependent way not to use system("cls")

will work with ncurses and PDCurses , although they might be overkill for small projects.

NCurses works for Unix and Linux and other POSIX systems, while PDCurses works for DOS, Windows, OS / 2 and some other random systems.

0


source


As mentioned earlier, there is no portable way to "clean" the screen. However, there is a portable way to "emulate" Windows system("pause")

, namely

std::cin.get(); // waits for ENTER

      

0


source


I don't have one for the system ("CLS"). But you can use the Sleep (int) function. It is not configured as a system ("PAUSE"), but you can configure the rest. The function pauses the program for as long as you like. Its parameter is an integer and according to its value the program sleeps. It is millisecond based. so Sleep (1000) pauses the program for 1000 milliseconds, or 1 second, because 1000ms = 1s.

 #include "iostream" // for the cout

 #include "windows.h" // for the Sleep() function

 using namespace std;

int main(){

cout << "hello" << endl;

Sleep(6000); // pauses program or puts it to 'sleep' for 6000 ms or 6 s


return 0;
} 

      

-2


source







All Articles