Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 21st, 2004, 6:13 AM   #1
gamehack
Newbie
 
Join Date: Sep 2004
Posts: 12
Rep Power: 0 gamehack is on a distinguished road
Send a message via ICQ to gamehack
Hello all,

I wanted to ask is there a way to clear the screen in my C++ program? I think that may be it is something platform specific.

Thanks very much
gamehack is offline   Reply With Quote
Old Nov 21st, 2004, 6:59 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I believe in Borland C++ there's a function called clrscr() in dos.h. In Windows, you can simply call system("cls") - this is the equivalent of typing "cls" (no quotes) at the command prompt. The system() function is in stdlib.h. And I'm not sure about Linux - you might be able to use the system function with that as well. Oh, and I've probably got something wrong, but someone'll be along to correct me in about 15 minutes or so.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 21st, 2004, 9:37 AM   #3
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>I wanted to ask is there a way to clear the screen in my C++ program?
Yes, though it may be the case where you have to work for it.

>I think that may be it is something platform specific.
Most definitely. The language definition doesn't require there to even be a screen, much less a way to clear it.

>I believe in Borland C++ there's a function called clrscr() in dos.h.
conio.h, actually.

>In Windows, you can simply call system("cls")
This generally isn't recommended for anything more than toy programs. The system function is standard and portable, but the argument you give it cannot be, so use of system gives a false sense of portability. Also, there are security issues where if the system doesn't have the shell command, a malicious program can be called instead of the expected program. This can also happen on systems where the command is available, but a malicious program is placed earlier in the search path, and therefore has priority. The third argument one of performance. The system function is dreadfully slow, and if it's called many times during execution, the effect could be detrimental.

For Windows, the suggested solution is to make use of the Win32 API. This is portable to all Windows compilers, and relatively quick:
#include <windows.h>

void clear_screen()
{
 DWORD c, sz;
 COORD coord = {0, 0};
 HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
 CONSOLE_SCREEN_BUFFER_INFO buf;
 GetConsoleScreenBufferInfo ( h, &buf ); 
 
 sz = buf.dwSize.X * buf.dwSize.Y; 
 FillConsoleOutputCharacter ( h, ' ', sz, coord, &c ); 
 GetConsoleScreenBufferInfo ( h, &buf ); 
 FillConsoleOutputAttribute ( h, buf.wAttributes, sz, coord, &c ); 
 SetConsoleCursorPosition ( h, coord ); 
}
On machines compatible with a VT terminal you can clear the screen with an escape sequence:
void clear_screen()
{
 cout<<'\x1b'<<"[2J"<<flush;
}
On Linux and Unix machines that have curses (or something similar) installed:
#include <curses.h> 

void initialize()
{
 initscr();
}

void clear_screen()
{
 clear();
 refresh();
}
There are dozens of ways to clear a console screen manually, but the above are the most common recommendations.
Eggbert is offline   Reply With Quote
Old Nov 21st, 2004, 2:39 PM   #4
Overmind
Professional Programmer
 
Overmind's Avatar
 
Join Date: Jun 2004
Location: South Africa, Johannesburg
Posts: 301
Rep Power: 5 Overmind is on a distinguished road
...off topic, but I didnt want to start a new topic for this:
is there anything in C++ to get the time and/or date from the computer?
__________________
[SIGPIC][/SIGPIC]
Overmind is offline   Reply With Quote
Old Nov 21st, 2004, 2:52 PM   #5
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>is there anything in C++ to get the time and/or date from the computer?
#include <ctime>
#include <iostream>

int main()
{
 time_t now = std::time ( 0 );
 std::cout<< std::ctime ( &now );
}
That's just the beginning though. The standard C++ library supports several functions and types designed for flexible date and time manipulation. Check your reference for the contents of the <ctime> header.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC