![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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 |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>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 );
}void clear_screen()
{
cout<<'\x1b'<<"[2J"<<flush;
}#include <curses.h>
void initialize()
{
initscr();
}
void clear_screen()
{
clear();
refresh();
} |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jun 2004
Location: South Africa, Johannesburg
Posts: 301
Rep Power: 5
![]() |
...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] |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>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 );
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|