***Clean screen!***

Team,

How to clean the console screen.

1
2
3
4
cout << choose;
cin  >> choice;
//Clean the screen.
cout << new text, depending on choice.
Try something like
std::cout << std::string{100, '\n'}; // print 100 newlines, clearing the screen.
Last edited on
closed account (1vf9z8AR)
#include<conio.h>
clrscr();

or

system("cls");
Last edited on
Sigh. I was about to congratulate you @suyashsing234 in the Lounge for seeing past over-complicated stuff, but now I think you are confusing complicated and correct.

C and C++ suffer from some warts that come from age -- and many of C++'s shortcomings come from C. That said, it is possible to do very succinct, but correct things in C++.

Using clrscr() is an old (pre-standard) function that is unlikely to work in any but a few compiler contexts and only on Windows. Using system("cls") is a positive security hole and a colossal waste of resources to do something as "simple" as clearing the screen.

The most succinct and correct way to do it is to use the OS API to accomplish this OS-specific goal. Lookey here, an Article all about it!
http://www.cplusplus.com/forum/articles/10515/

And here's one about abusing system() for stupid cruft.
http://www.cplusplus.com/forum/articles/11153/

@shycas2008
Hope this helps.
While this isn't really an answer, terminal user interfaces were always of limited utility. The terminal is suited for non-interactive applications, where doing things like "clearing the screen" is at least hardly useful, usually annoying, and at worst crippling to the user's ability to automate his tasks.

C++ is quite sufficient for writing simple command line programs with relatively little extra boiler-plate, but it seems every other post refers to a program designed for interactive use. In my opinion, this is a computer literacy problem - such applications seem to be designed by people with very little practical knowledge about the command line.

Before clearing the screen, you should ask yourself if you really need to prevent the user from reading the previous output from his terminal session.

TUIs are not entirely useless: top(1), for example, makes a good case study:
http://man7.org/linux/man-pages/man1/top.1.html
Last edited on
closed account (1vf9z8AR)
@duthomhas
I never thought about cross compiler coding:(
Thanks though.
Last edited on
Sorry, I find myself sounding harsh a lot when it is not intended.

In the CS world, you will find a wide range of quality in code, ranging from all-too-common cruft to highly-visible works of art.

When you get on a forum, the people in the know are interested in work-of-art quality code. And in that case, all complexity exists because it is necessary.

Hope this helps.
I used suyashsing234's example.
I'm writing on Linux and Windows.

Excellent help all, thank you!!!

system("clear"); --> Linux
system("cls"); --> Windows

Trying to teach myself how to do this - one step at a time.
I didn't realize you could introduce native shell commands!
A total game changer.

Everyone here is awesome.





Topic archived. No new replies allowed.