I've been using system("cls") to clear a DOS window for a while now, but I don't want to use system() commands any more. Is there a more standard way I could do this? I tried using gotoxy(int x, int y) style functions, where I would go to the first character of each line (gotoxy(i, 0), where i is the current iteration in a loop), and writing about 50 spaces but it did not work.
Is there another way?
If you didn't understand my first paragraph, this is what I meant
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void gotoxy(int x, int y) {
COORD coord; // coordinates
coord.X = x; coord.Y = y; // X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void clrscr() {
int i;
for (i=0; i<100; i++) {
gotoxy(i, 0);
cout << " "; /* Print 50 spaces */
}
gotoxy(0,0);
}
But, while it sort of worked, it takes too long to do whereas system("cls"); is virtually instantaneous.
In the article, there's something in there similar to what you did. It just call a line break 100 times to clear the screen but that's easily not the best way so I don't recommend it.
BTW, I should note that in my articles, you should often take the hardest look at the "OS-specific ways" -- they are usually what you really want to do.
i think printing 50 useless space is probably more problimatic than just using system("cls");
its one of those things you cant just stop using because people tell you too, just use it until it gives you issues. my 2 cents
helios is right... none of the system commands anyone has posted so far in the forums works when I run their code on my terminal. Printing 50 lines may be grossly inelegant, but at least it works on all platforms.
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__)
/* This here be the Windows method */
#ifndef __WIN32__
#define __WIN32__
#endif
#include <windows.h>
void ClearScreen()
{
...
}
#elif defined(unix) || defined(__unix) || defined(__unix__)
/* And this is the POSIX method.
* Keep in mind that not all compilers #define one of the above macros.
* You're compile script will have to make sure it is #defined before compiling.
*/
#include <unistd.h>
#include <term.h>
void ClearScreen()
{
...
}
#else
/* And if you are not using Windows or POSIX
* but some weird, ancient system like a VAX.
*/
#ifdef __cplusplus__
#include <iostream>
#include <string>
void ClearScreen()
{
cout << string( 100, '\n' );
}
#else
#include <stdio.h>
void ClearScreen()
{
int n;
for (n = 0; n < 10; n++)
printf( "\n\n\n\n\n\n\n\n\n\n" );
}
#endif
#endif
This is just a simple, one-file example. It is normal for large projects to have OS-specific files for doing things (like clearing the screen), and an appropriate makefile for each architecture, where the correct files are #included and linked to the executable.
The reason I printed fifty spaces several times was because I'm never going to have printed > 50 lines. At most, I'll have used 20. Usually I clear after every time I start something new - example: If there was a level in a game I would clear the screen after every 'checkpoint' in the level and at the beginning and end of each level. I wouldn't use so much text that they'd have to scroll down.