Sign in to follow this  

How do I clear the screen in a console app (c++)

This topic is 4713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I'm not writing a game but I figured this is the best place for a piece of advice. How do I simply clear the screen? I know there is a command. I just don't know it. :) Thank You.

Share this post


Link to post
Share on other sites
Printing out lots and lots of spaces and/or newlines.

Unfortunatley I don't know of a command that works on both unix and windows.

Course you could always get the user to create an alias that alias one to the other, but yes this is a stupid idea.

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
Or system("clear")
Doesn't seem to work with Mingw/GCC.

#include <iostream>

main()
{
std::cout << "Hello World...\n\nPress Enter.";
std::cin.get();
std::system("cls");
std::cout << "Once Again, Hello World!\n\nPress Enter.";
std::cin.get();
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Rob Loach
Quote:
Original post by Boder
Or system("clear")
Doesn't seem to work with Mingw/GCC.

*** Source Snippet Removed ***

It is not up to the compiler, it is up to the system.
"clear" isn't a command in my windows too.

Share this post


Link to post
Share on other sites
The correct one is as Meta Adam said, its system("cls") as cls is the clear command for the windows command prompt (carried over from the old dos operating systems) clear is for linux systems.

Share this post


Link to post
Share on other sites
Maybe something like this, but I don't know how to detect for a linux system, so if anyone wants to help me out with the preprocessor instructions.


#ifdef __WIN__32
#define CLEAR_SCREEN system("cls);
#else /*elif ...*nix*/
#define CLEAR_SCREEN system("clear");
#endif

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
cout << "Welcome, attempting to clear screen" << endl;
cin.get();
CLEAR_SCREEN
cout << "You like that? Seeya later." << endl;

return 0;
}

[/source

Share this post


Link to post
Share on other sites
I think you would want to do it like this:


#ifdef WIN32 //I think this is the right one
#define CLEARSCREEN "cls"
#else
#define CLEARSCREEN "clear"
#endif

int main() {
cout << "Hello y'all!";
system(CLEARSCREEN);
return 0;
}




EDIT: btw, what do I need included to use system()?

Share this post


Link to post
Share on other sites
Quote:
i hate it how ther's no universal solution (cross platform) to this common problem.. or is there?


No there isn't. The closest you can come without being platform-dependent is the print a bunch of newlines method.

C++ is basically a really fancy extenstion to C. The thing to remember about I/O in C is that it was designed in a time when your typical interactive I/O device was a teletype - basically a suped-up typewriter (phea4 teh l33t ness. Thus C has no support for anything more than what a teletype can do. Teletypes have no screen and thus no support in the language for clearing the screen. If you want language support for this sort of thing you need to change languages.

It also has to do with the idea that "everything is a file". All I/O in C basically pretends it is talking to a file - there are even built-in names for them - stdin/stdout/stderr. You can't clear the screen of a file.

Using system() to execute OS calls, calling OS api's directly, curses, VT100 escapes, ANSI escapes, whatever will all fail on one relatively popular config or another.

Share this post


Link to post
Share on other sites

This topic is 4713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this