15

I would like to clear the CMD screen. I have seen a few options. First is

system("cls");  

but I don't want to use system, cause then it makes it dependent on Windows. Same with the Unix version.

If I try

cout << string(22, '\n');

then my next line of text is at the bottom of the screen and I want it at the top. How can I clear the screen and get the text back to the top of the screen?

Thus say I have this:

cout << string(22, '\n');
cout << "************Question 1 *******" << endl;
cout << "WHO WAS THE FIRST BLACK PRESEDENT?" << endl;
cout << "(1) Obama" << endl;
cout << "(2) Bush" << endl;
cout << "(3) Jordan" << endl;
cin >> answer >> endl;

This will clear the screen then put mymenu at the bottom of the screen... How can I make it clear the screen and put the question/answers back up top of the screen?

wovano
  • 4,543
  • 5
  • 22
  • 49
Glen Morse
  • 2,437
  • 8
  • 51
  • 102
  • Have you looked into some solution like ncurses/PDCurses? It's not exactly cross-platform but it would do what you want. – CmdrMoozy Jun 27 '13 at 06:20
  • 3
    what about `clrscr()` –  Jun 27 '13 at 06:21
  • i thought clrscr() was a compiler only function... as in just borland? – Glen Morse Jun 27 '13 at 06:21
  • 5
    `clrscr()` is typically defined in `conio.h`, which is generally only available on Windows (http://en.wikipedia.org/wiki/Conio.h) - I would argue that it is approximately as portable as the OP's `system('clr');` solution. – CmdrMoozy Jun 27 '13 at 06:22
  • a standard C++ function would be best , but could not find one. – Glen Morse Jun 27 '13 at 06:23
  • 4
    possible duplicate of [How do you clear console screen in C?](http://stackoverflow.com/questions/2347770/how-do-you-clear-console-screen-in-c) – Christian.K Jun 27 '13 at 06:25
  • this is C++ and not just how to clear screen but also move cursor / text up. – Glen Morse Jun 27 '13 at 06:28
  • if you clear the screen, the cursor automatically goes to the top of the screen – Aswin Murugesh Jun 27 '13 at 06:28
  • @GlenMorse The same answer applies, therefore it is a duplicate. – lesderid Jun 27 '13 at 06:29
  • nothing on that page answers the question, yes but how you clear the screen? @aswinMurugesh – Glen Morse Jun 27 '13 at 06:30
  • 2
    Here's the most complete guide to "clearing the screen" in console mode that I found: http://www.cplusplus.com/articles/4z18T05o/ – Marcello Romani Jun 27 '13 at 06:33
  • There is not such `concrete platform independant` solution for this problem – Aswin Murugesh Jun 27 '13 at 06:33
  • @GlenMorse Maybe it doesn't have an answer that you like, but it does have valid answers that continue to apply for this question. – lesderid Jun 27 '13 at 06:34
  • 1
    Glen, the accepted answer to the other question starts: *Well, C doesn't understand the concept of screen. So any code would fail to be portable.* Replace C with C++ and it applies to your case. – Gorpik Jun 27 '13 at 06:35
  • 1
    @marcelloRomani perfect! i can use that once i detect the platform there using, as answer said there was not standard function and this seems to follow what he said. – Glen Morse Jun 27 '13 at 06:35
  • @Gorpik great! but that does not change the fact that they are the same question! I am sure there is alot of difference in the two programs, never used c but if it was exactly the same as C++ then they would both have the same name. – Glen Morse Jun 27 '13 at 06:37
  • @GlenMorse: That's a rather naive statement (assuming I parsed it correctly). I suggest you read about the history of C++. – MatthewD Jun 27 '13 at 06:58
  • @matthewD it may have come from C but still there is alot c++ can do that c can not – Glen Morse Jun 27 '13 at 08:12
  • if fact this comment section seems "Overloaded" ... did you see what i did there.. – Glen Morse Jun 27 '13 at 08:18
  • 1
    @GlenMorse: You can write code in C and call it from C++. And you can write code in C++ and call it from C. All of the solutions in the answer you marked as correct are actually libraries written in C. – MatthewD Jun 27 '13 at 09:51
  • Sure, C and C++ are not the same language. But C is (roughly) a subset of C++ and system calls tend to fall inside that subset. – Gorpik Jun 27 '13 at 10:00
  • exactly a subset, thus a chance that c++ can do something where c canNOT is more then possable and happens! – Glen Morse Jun 27 '13 at 10:24
  • The extra parts in C++ just allow you to write the same code using different styles (eg. OOP). It doesn't mean C is less capable. Anything you can write in C++ you can also write in C, you just write it differently, that's all. – MatthewD Jun 28 '13 at 01:44
  • that can be said for any language... – Glen Morse Jun 28 '13 at 03:47

4 Answers4

38

Try this: it works both on Linux and Windows.

cout << "\033[2J\033[1;1H";

This is a string of special characters that translate to clear the screen command.

You can enclose this in a function like e.g. clrscr() depending on your implementation.

baduker
  • 19,152
  • 9
  • 33
  • 56
catzilla
  • 1,901
  • 18
  • 31
8

If you want a solution that will work on Windows, Mac & Linux/UNIX, you will need to come up with your own implementation. I do not believe that there is a single way to do it that works on all platforms.

For Mac/Linux/UNIX/BSD/etc., ncurses provides an easy way to do this (http://www.gnu.org/software/ncurses/).

For Windows, you will probably want to look into conio.h (http://en.wikipedia.org/wiki/Conio.h) or PDCurses (http://pdcurses.sourceforge.net/) or something similar. Alternatively, it would seem that you can do this without any third-party libraries, according to this Microsoft KB article: http://support.microsoft.com/kb/99261.

There is unfortunately no standard C/C++ function to do this. You should be able to write a small function which will build & work on any platform using the different methods I mentioned and some preprocessor directives.

If you don't have a convenient way to detect the platform, I would probably recommend cmake.

CmdrMoozy
  • 3,870
  • 3
  • 19
  • 31
  • So no standard function, thus i must detect the platform, then use one of these two options based on what i detected? – Glen Morse Jun 27 '13 at 06:33
  • 2
    As far as I know, yes. I would expect the result to be only a hundred lines of code or something, and you should be using a build system which can detect the platform anyway, so this is probably not as "ugly" an option as you might think. – CmdrMoozy Jun 27 '13 at 06:34
  • going to give you answer before some dumb people that think c and C++ are exactly the same program and just cause one does not do something then the other must not. – Glen Morse Jun 27 '13 at 06:41
0

In UNIX try

system("clear")

clrscr() may not work in UNIX because some compilers do not support conio.h

Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
0

Another way would be to use OpenGL, Qt or SDL, which are cross-platform and write a graphical console. This can be seen in many roguelike games, for example Dwarf Fortress.

user1095108
  • 14,119
  • 9
  • 58
  • 116
  • This would work, but it should be noted that this would probably require significantly more effort than other solutions. It does give you more finite control, though, so depending on what your end goal is it could be worthwhile. – CmdrMoozy Jun 27 '13 at 06:39
  • This is also a great option, and answers like this is what i was hopeing for, but just happens in this case i will not be using openGL. But thanks for shareing the idea – Glen Morse Jun 27 '13 at 06:42