| | |
Clearing Screen in the console
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
Hey, I'm brand new to this site. Hopefully someone can help me out.
How do you clear the console screen without any system-compatibility issues?
This means that SYSTEM("CLS") and anything from a windows library (like #include<windows.h>) is not acceptable, as it will not run on linux/unix etc...
I know theres some tweaks you can do to linux which would make this work, but this is for a computer engineering course, and it has to run on everything.
I've created a function that looks something like this:
While this does pseudo-solve my problem, it leaves the text on the bottom of the console screen rather than the top, and you can just scroll up to see the previous output. It seems a little redundant to clear the screen in the console, but I need it for a lab at school. Although I don't think anyone else in my class has figuired out how yet...
Thanks.
How do you clear the console screen without any system-compatibility issues?
This means that SYSTEM("CLS") and anything from a windows library (like #include<windows.h>) is not acceptable, as it will not run on linux/unix etc...
I know theres some tweaks you can do to linux which would make this work, but this is for a computer engineering course, and it has to run on everything.
I've created a function that looks something like this:
c++ Syntax (Toggle Plain Text)
void CLS() { for (int iLoop = 0; iLoop < 40; iLoop++) { cout << " "; } return 0; }
While this does pseudo-solve my problem, it leaves the text on the bottom of the console screen rather than the top, and you can just scroll up to see the previous output. It seems a little redundant to clear the screen in the console, but I need it for a lab at school. Although I don't think anyone else in my class has figuired out how yet...
Thanks.
There is no way to do this without system dependencies. C++ doesn't know anything about the output device, so if you want to play with it you have to know something about it.
It is possible to write your C code to choose to compile specific options depending on what OS you are using, but that is a rather clunky option.
Another option is to write several separate cpp files that each define the same function, but implement it differently depending on the OS. Compile and link the one appropriate to the OS. (This is the most common option.)
For just playing with the console though, I recommend you to look at the curses library. There are several versions: curses, ncurses, and pdcurses. I recommend the latter. It is available on most POSIX capable systems, including windows.
If you want something really simple, and you have ANSI.SYS running on your Windows PC, you can just code:
If it works it works. If not you'll just get some junk on the screen.
Sorry I couldn't be of more help.
It is possible to write your C code to choose to compile specific options depending on what OS you are using, but that is a rather clunky option.
Another option is to write several separate cpp files that each define the same function, but implement it differently depending on the OS. Compile and link the one appropriate to the OS. (This is the most common option.)
For just playing with the console though, I recommend you to look at the curses library. There are several versions: curses, ncurses, and pdcurses. I recommend the latter. It is available on most POSIX capable systems, including windows.
If you want something really simple, and you have ANSI.SYS running on your Windows PC, you can just code:
cout << "\33[2J";If it works it works. If not you'll just get some junk on the screen.
Sorry I couldn't be of more help.
>How do you clear the console screen without any system-compatibility issues?
Start by altering reality such that standard C++ recognizes a "screen" and a "console" in a convenient way.
>but this is for a computer engineering course
I'm impressed. Most programming courses actually take the opposite stance when it comes to portability.
>and it has to run on everything.
It's quite impossible to do that. The second best is to pick which systems you want to be compatible with, isolate the non-portable aspects of your code, and port (or conditionally compile) the non-portable parts as necessary to get the code working on the different systems that you're supporting. For example, here's a somewhat naive way to go about handling both Windows and POSIX systems:
Start by altering reality such that standard C++ recognizes a "screen" and a "console" in a convenient way.
>but this is for a computer engineering course
I'm impressed. Most programming courses actually take the opposite stance when it comes to portability.
>and it has to run on everything.
It's quite impossible to do that. The second best is to pick which systems you want to be compatible with, isolate the non-portable aspects of your code, and port (or conditionally compile) the non-portable parts as necessary to get the code working on the different systems that you're supporting. For example, here's a somewhat naive way to go about handling both Windows and POSIX systems:
C++ Syntax (Toggle Plain Text)
#include <cstdlib> void clear_screen() { #ifdef WINDOWS std::system ( "CLS" ); #else // Assume POSIX std::system ( "clear" ); #endif }
New members chased away this month: 4
In other words, brute force. Outputting a bunch of newlines '\n' is the only portable way.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> How do you clear the console screen without any system-compatibility issues?
when the language (and the language libraries) do not provide a portable way of doing something, the usual approch is to look for a library with an api, allowing the programmer to write code in a portable way. ncurses is one that you could use. http://tldp.org/HOWTO/NCURSES-Progra...WTO/intro.html
unix/linux : usually available as part of your base installation. if not, obtain from ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz
windows: download from http://gnuwin32.sourceforge.net/packages/pdcurses.htm
when the language (and the language libraries) do not provide a portable way of doing something, the usual approch is to look for a library with an api, allowing the programmer to write code in a portable way. ncurses is one that you could use. http://tldp.org/HOWTO/NCURSES-Progra...WTO/intro.html
unix/linux : usually available as part of your base installation. if not, obtain from ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz
windows: download from http://gnuwin32.sourceforge.net/packages/pdcurses.htm
>Outputting a bunch of newlines '\n' is the only portable way.
It's portable only in that you're using standard code. It's not portable in that you don't know how many newlines to print, and if the stream is redirected (or never wrote to a screen in the first place), you're not going to be clearing any screens. "Clearing the screen" requires a screen and knowledge about that screen, both of which are fundamentally non-portable.
>Wow, such a simple concept, yet so many unclear answers.
No, it only seems like a simple concept because you're ignoring all of the variables involved. Clearing the screen is actually a complex topic, which is precisely why the language definition doesn't specify anything about it.
>Oh well, I'll talk to my teacher about it, maybe it was a trick question
I doubt it. Methinks your teacher wants you to use fflush(stdin).
>or maybe she just meant Linux and Windows.
In that case, see my previous reply.
It's portable only in that you're using standard code. It's not portable in that you don't know how many newlines to print, and if the stream is redirected (or never wrote to a screen in the first place), you're not going to be clearing any screens. "Clearing the screen" requires a screen and knowledge about that screen, both of which are fundamentally non-portable.
>Wow, such a simple concept, yet so many unclear answers.
No, it only seems like a simple concept because you're ignoring all of the variables involved. Clearing the screen is actually a complex topic, which is precisely why the language definition doesn't specify anything about it.
>Oh well, I'll talk to my teacher about it, maybe it was a trick question
I doubt it. Methinks your teacher wants you to use fflush(stdin).
>or maybe she just meant Linux and Windows.
In that case, see my previous reply.
New members chased away this month: 4
![]() |
Similar Threads
- Waitkey command (C++)
- Full screen console applications (C++)
- Clearing the screen (C++)
- Clear screen (C++)
- substitute for conio.h in linux (C)
- Some Simple Console App Questons (Java) (Java)
- Clearing the screen (C++)
Other Threads in the C++ Forum
- Previous Thread: consts in a header file...
- Next Thread: Dijkstra's Algorithm help
Views: 6485 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding stream string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






