Is there an easy way to clear the screen / hold the output window in the CLI using compatible code, avoiding conio.h and ncurses.h?

I just started to study computer science. A programming teacher chose C ++ to teach us process programming and, therefore, gives us code samples and exercises. The first sample we got is the CLI game "Find the Right Number". In fact, the first task was to simply retype the source code, compile it, and run it. I have been using Linux for several years (even if I'm not a computer nerd at all), and when I compiled the code, it failed. Looking at the error logs, I noticed that my teacher does not seem to be considering Unix users.

She calls the library conio.h, which is the old headline for MS-DOS compilers, as Ive read there: https://stackoverflow.com/a/166906/ ...
I got two other errors due to use system("cls");and getch();which are functions only for Windows, from that that Ive found out.

I could not find suitable alternatives for these functions during my research. At least the hard decisions that fit my current level of programming. I was considering using it ncurses.h, but Ive read this page , which is really interesting, but NCurses is portrayed as overkill ...

So, is there a cross-platform way to clear the screen and keep the output window until the user presses a key? Is NCurses redundant, as they say, or is this the best solution at the moment?
I think I will have to do such CLI things in upcoming assignments. I could, of course, just use the MS-only functions so that it doesn't complain, but Id would rather be able to create compatible code.

+4
source share
1 answer

The most common and portable way to clear a console window is to simply write a function to output a bunch of newline characters, for example:

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

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

void pause() // wait for input and discard any unnecessary input 
{
    std::cin.get(); 
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
+4
source

Source: https://habr.com/ru/post/1654029/


All Articles