Clearing the consule window in C++

amazingtrade

Mad Madchestoh fan
Joined
Jun 19, 2003
Messages
5,139
Reaction score
0
Location
Manchester
I want to tidy up my UI for my program by clearing the screen to make it look more tidy on the consule mode in C++. I am using the IOSTREAM libary. Does anybody know how you clear the consule screen in C?

I want to the CLS thing in BASIC.

Thanks for any help, I have tried google and books but found nothing.
 
what compiler are you useing?

if it supports conion.h (console input output)

Code:
#include <conio.h>

int main()
{

           printf("arse hats");
           getchar();
           clrscr();

           getchar();
           return;
}
ish

clears the console screen and returns the cursor to top left.
the library also contains gotoxy, usefull for makeing a tidy UI


conio.h is not ANSI C so be careful if portability is an issue
 
Last edited by a moderator:
Thanks I will have a look at that. I'm using C++ NET at uni but Bloodshed DEV C++ at home so portability is a major issue.
 
Originally posted by amazingtrade
Thanks I will have a look at that. I'm using C++ NET at uni but Bloodshed DEV C++ at home so portability is a major issue.
If you're coding for the Windows environment then the code in the link I gave you will be fine but it's strictly Windows environment.

Michael.
 
why not just print lots of \n's the scroll everything off the top of the console? should port to other platforms well enough?
int scrLines = 30;
for (int i = 0; i < scrLines; i++) cout << "\n";
should do it (for a 30 line console) or is my console c++ that rusty?

cheers


julian
 
I thought of doing something like that but not shure how that would work in a windows enviroment as it /n makes it scroll down rather than up.

That will just add a long scrollbar to the screen and leave lots of white space I think.

I did so somthing like that in Java in college but I was working in a DOS environment then.

I wlll try it later though when I am on my dekstop, I am in bed atm on my ancient P233 laptop.
 
I try it, I am sure its pretty easy to move the postion of the cursor using x,y commands but then I might as well use Micheal's suggestion as that involves using other libaries too.

I will have a play.
 
You could read the Windows documentation, this is immediately relevant. http://msdn.microsoft.com/library/en-us/dllproc/base/clearing_the_screen.asp

If you're writing Windows apps I think you could find a free 'curses' implementation for Windows. This would give you Unix source compatibility but is a whole new thing to learn.

But if all you want to do is clear the screen when your program starts/exits then the 'system ("cls");' approach rules. If you have compilation problems then just use conditional compilation to remove the call from the Bloodhound build.

Paul
 
have you tried
2384.jpg
 
If I wasn't feeling so down (because of a couple of problems at uni and my social life crisis) I would actually find that quite funny:)
 

Latest posts

Back
Top