Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a simple alternative to system("CLS")?

Tags:

c++

After reading why system() is evil, I decided to not use features like system("CLS") and system("PAUSE"). Are there any easy alternatives that aren't OS dependent?

like image 750
Hunter George Avatar asked Oct 25 '14 14:10

Hunter George


2 Answers

There are two ways:

Creating a function:

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

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

Also the only other non-OS dependent way to not use system("cls") would be working with ncurses and PDCurses, although they can be overkill for smaller projects.

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

like image 59
Chantola Avatar answered Sep 30 '22 21:09

Chantola


As mentioned before, there is no portable way of "clearing" the screen. There however a portable way of "emulating" Windows' system("pause"), namely

std::cin.get(); // waits for ENTER
like image 21
vsoftco Avatar answered Sep 30 '22 19:09

vsoftco