User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,442 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,626 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2464 | Replies: 10
Reply
Join Date: Nov 2007
Posts: 243
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Rep Power: 3
Solved Threads: 23
skatamatic skatamatic is offline Offline
Posting Whiz in Training

Clearing Screen in the console

  #1  
Nov 2nd, 2007
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:
  1.  
  2. void CLS()
  3. {
  4. for (int iLoop = 0; iLoop < 40; iLoop++)
  5. {
  6. cout << " ";
  7. }
  8. return 0;
  9. }

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Clearing Screen in the console

  #2  
Nov 2nd, 2007
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:
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 243
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Rep Power: 3
Solved Threads: 23
skatamatic skatamatic is offline Offline
Posting Whiz in Training

Re: Clearing Screen in the console

  #3  
Nov 2nd, 2007
Hmmm, I think I'll have to do some research on determining the OS. I could probably just run a script that looks for key OS files (in the windows system folder, etc) and if they exist, clear the screen one way, and if they don't, clear the screen another. Thanks for the help!
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 488
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Clearing Screen in the console

  #4  
Nov 2nd, 2007
>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:
  1. #include <cstdlib>
  2.  
  3. void clear_screen()
  4. {
  5. #ifdef WINDOWS
  6. std::system ( "CLS" );
  7. #else
  8. // Assume POSIX
  9. std::system ( "clear" );
  10. #endif
  11. }
I'm here to prove you wrong.
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Clearing Screen in the console

  #5  
Nov 2nd, 2007
Originally Posted by Narue View Post
>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.

In other words, brute force. Outputting a bunch of newlines '\n' is the only portable way.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Clearing Screen in the console

  #6  
Nov 2nd, 2007
Originally Posted by WaltP View Post
In other words, brute force. Outputting a bunch of newlines '\n' is the only portable way.


And that isn't portable either because there is no standard way to determine how many line to clear.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 243
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Rep Power: 3
Solved Threads: 23
skatamatic skatamatic is offline Offline
Posting Whiz in Training

Re: Clearing Screen in the console

  #7  
Nov 2nd, 2007
Haha. Wow, such a simple concept, yet so many unclear answers. Oh well, I'll talk to my teacher about it, maybe it was a trick question, or maybe she just meant Linux and Windows. Thanks all.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Clearing Screen in the console

  #8  
Nov 2nd, 2007
All the answers given you have been very clear. Some even posted code that works just fine.

What's not too clear is your teacher's brain if she gave you a homework assignment to clear the screen using C++...
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Clearing Screen in the console

  #9  
Nov 3rd, 2007
> 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
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 488
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Clearing Screen in the console

  #10  
Nov 3rd, 2007
>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.
I'm here to prove you wrong.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC