More 'standard' system("cls")?

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.
http://www.cplusplus.com/forum/articles/10515/
:-)

[edit] arrgh.. quit stalking me! J/K XD
Last edited on
closed account (z05DSL3A)
Clear the screen
http://www.cplusplus.com/forum/articles/10515/
closed account (S6k9GNh0)
http://www.cplusplus.com/forum/articles/10515/

I think I'm going to make a site dedicated to things like this...
Thanks everyone. I love that you all posted the same link...
closed account (S6k9GNh0)
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
i think printing 50 useless space is probably more problimatic than just using system("cls");
Considering printing 50 lines will work anywhere (where the console has at most 50 lines), but system("cls"); won't, I don't think so.
you got me
closed account (S6k9GNh0)
I either due the OS based version or I use NCurses...I don't really code in command based apps unless I'm testing a theory anyways so meh.
yeah, dos applications are quiet boring, but thats where most people start
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.
Sigh.

Here are the two OS-specific ways to do it on 90% (made-up statistic) of all platforms.
http://www.cplusplus.com/forum/articles/10515/page1.html#msg49080

All it takes is some proper macro branches to account for your hardware
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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.

Hope this helps.
What about MacOS <10?
A system that has no console?
It doesn't? I would have assumed it did, but I don't really know.
Thanks for the help.

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.
Last edited on
Topic archived. No new replies allowed.