PDA

View Full Version : [C++]Clearing the console - Cross Platform



anothrguitarist
August 13th, 2007, 11:14 PM
I am making a simple text tic tac toe game, and I was wondering how to clear the console window. Also, I want this game to work on windows as well, so is there any cross-platform method for clearing the console window and setting the cursor back to 0,0?

Regards,
- John

anothrguitarist
August 15th, 2007, 06:19 AM
Well I found a non cross platform method of doing this:

system("clear")

And if I want to create a windows version of this program, I can just edit a few lines.

slavik
August 15th, 2007, 07:09 AM
read about ncurses. :)

Wybiral
August 15th, 2007, 07:25 AM
read about ncurses. :)

NCurses is really good, but I'm not entirely sure if there is a windows port of it. I could be wrong, but at first glance it looks like there is none.

slavik
August 15th, 2007, 07:28 AM
probably not, but it beats printing stuff over and over again ...

Wybiral
August 15th, 2007, 07:42 AM
probably not, but it beats printing stuff over and over again ...

I'll second that. If you plan to write some sort of UI in the terminal, you'll be much better off using a library like ncurses then to use all the work-arounds required to make simple printed output look right.

LaRoza
August 15th, 2007, 01:46 PM
Well I found a non cross platform method of doing this:

system("clear")

And if I want to create a windows version of this program, I can just edit a few lines.

You can use:



#ifndef WINDOWS
system("clear");
#else
system("cls");
#endif


When you compile it for Windows, just just define Windows:


#define WINDOWS

anothrguitarist
August 16th, 2007, 12:04 AM
Thanks all :)

slavik
August 16th, 2007, 12:54 AM
there is actually a #define that the compiler does ... check the gcc headers (__WIN32 I think)

this way, you don't even have to do any extra work. :)

anothrguitarist
August 16th, 2007, 02:21 AM
Got it. So, instead of ifndef windows, use __WIN32... Although it is a console program, do I need to check for __WIN64 too?

Thank you this makes things much better.

Edit: Err.. I'll check the headers to see what I find.

anothrguitarist
August 16th, 2007, 09:43 AM
Works like a charm on both windows and ubuntu.

Cheers,
- John