Poulpette Poulpette - 1 year ago 62
C++ Question

Is there a simple way to clean screen/hold the output window in CLI with an interoperable code, avoiding conio.h and ncurses.h?

I have just started computer science studies. The programming teacher chose C++ to teach us procedural programming, and therefore gives us code samples and exercises. The first sample we got was a CLI “Find the right number” game. The really first assignment was to simply retype the source code, compile it and run it. I have been using Linux for several years now (even though I’m not a computer nerd at all), and when I compiled the code, it failed. Looking at the error logs made me notice that my teacher is apparently not considering Unix users.

She calls the

library, which is an old header for MS-DOS compilers, as I’ve read there: http://stackoverflow.com/a/8792443/6723830

I got two other errors because of the use of
, which are Windows-only functions, from what I’ve learned.

I was not able to find suitable alternatives for those functions during my researches. At least not easy solutions to suit my current programming level. I was considering using
, but I’ve read this page, which is really interesting, but NCurses is depicted as overkill…

So, is there a cross-platform way to clean the screen and hold the output window until the user hits any key? Is NCurses as overkill as it is said to be, or is it the best solution for the moment?

I will have to make such CLI things in upcoming assignments, I guess. I could, of course, simply use MS-only functions so that she wouldn’t complain, but I’d rather be able to produce interoperable code.

Answer Source

The most common and portable way to clear the console window is to simply write a function to output a bunch of newline chars, something like:

void clearScreen()
    for(int i = 0; i < 15; ++i)
        std::cout << '\n';

And to hold input you can write a pause function, something like:

void pause() // wait for input and discard any unnecessary input 
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download