Sorry about the typo in title. Ok my school is addicted to making us mad and have blocked command promp that I brought my copy from home on a floppy disk and it blocked me. So what I need is a few alternatives for these:
cls - clears the screen
name of a program- runs the spesiphued program in he same directory
That all I use it for so help please!

Recommended Answers

The best alternative is not to clear the screen. Most of the time when I see things like system("cls") or clrscr() , there's no legitimate reason for clearing the screen.

However, since you're using Windows you might consider the Win32 API as a reasonably portable (on Windows) …

Jump to Post

For ultimate portability you have two options:

  1. Don't use non-portable features
  2. Implement the non-portable feature for every target system

In other words, don't clear the screen and don't go full screen if you don't want to write a Windows-specific build, a Linux-specific, and a Mac-specific build.

Jump to Post

Oh, why can't I just extract the code widows runs when cls is called and put that in my program?

Go for it...

Jump to Post

All 10 Replies

The best alternative is not to clear the screen. Most of the time when I see things like system("cls") or clrscr() , there's no legitimate reason for clearing the screen.

However, since you're using Windows you might consider the Win32 API as a reasonably portable (on Windows) solution. If you're dead set on clearing the screen, that is:

#include <windows.h>

void clear_screen()
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO buf;
    
    GetConsoleScreenBufferInfo(h, &buf);
    
    DWORD size = buf.dwSize.X * buf.dwSize.Y;
    COORD pos = {0};
    DWORD n;
    
    FillConsoleOutputCharacter(h, TEXT(' '), size, pos, &n);
    FillConsoleOutputAttribute(h, buf.wAttributes, size, pos, &n);
    SetConsoleCursorPosition(h, pos);
}

I already use cls the reason is that I want it to be able to be ported to many different computers aka school network (there going to use it as a actual finace tracking/simulation thing because it's not the usual flavor of these sort of things) and since half of our school uses apple some on win and a few more on Linux~ I also make it go fullscreen so text tends to get stuck at the bottom of the screen and /n doesn't fix that.
Edit: oh yeah I remembered my idea: could I just get the code from cmd.exe or wherever system commands go and put in my program for ultamate portability

For ultimate portability you have two options:

  1. Don't use non-portable features
  2. Implement the non-portable feature for every target system

In other words, don't clear the screen and don't go full screen if you don't want to write a Windows-specific build, a Linux-specific, and a Mac-specific build.

Oh, why can't I just extract the code widows runs when cls is called and put that in my program? Mainly how do I tell what kind of os the person is using from my program, I think I hard about it but can't really type that into google.

Oh, why can't I just extract the code widows runs when cls is called and put that in my program?

Go for it...

Wait I can really do that!?! How? PS how do I tell what os the user is using I just can't find it in the mountain of papers in my backyard it's called lone mountain(yes thé one in Vegas); it's actualy all just my reference papers and encyclopedias.

Compiler translates your code to native machine code. You cannot have a compiled binary which runs on all the platforms.

True for C and C++. Experts please correct me if I'm wrong.

Wait I can really do that!?!

No. I only said that because you won't accept that a portable CLS cannot be done.
Honest, it cannot be done.

Ok :( then what is the equivalent of cls on the other two giants: OSX and LINUX

system("clear") is the equivalent to system("cls") on both Linux and OSX.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.