The question is in the title. Rather then making a call to the OS, is there a more efficient way to do this? Thanks

Edit: Just though of something else to.
Does Sleep(2000); make a call to the OS as well? If so is there another way to do this as well?

Both sleep and cls are non-portable.

There is no portable method to clear the screen

Thats because you cant guarauntee the output device is nececerially the screen. e.g on an embedded device it could be a typewriter or something

>is there a more efficient way to do this?
You mean without moving away from the console for non-sequential output? You could try pdcurses or something similar. At least that way you have more control, but if you're doing any kind of GUI-like interface or advanced drawing, you'd be better off taking the plunge into a modern graphics API.

>Does Sleep(2000); make a call to the OS as well?
How exactly are you expecting to talk to the system or any peripherals without making a call to the OS?

> How exactly are you expecting to talk to the system or any peripherals without making a call to the OS?

On some operating systems and some devices, you can do just that.

Comments
lmao

>On some operating systems and some devices, you can do just that.
Yes, but on the OP's operating system and device? Unlikely.

Notes on system() and equivalent functions.

A guy walks up to a rack of fireworks blindfolded. After a short period of time, he mutters something and there's a huge explosion.

First Onlooker: What did he say?
Second Onlooker: I think it was "Is it lit yet?"

I think there is no flaw in using this system call but that it is platform dependent.
Also c++.com has no other function for the same
Actually you just write system("any dos command"); in windows.

But in linux::
1)u will have to include syscall.h
2)in linux the command to clearscreen is not cls but clear.

I think one good way to try to make system calls portable is to simply put them in a separate library file within a custom made function. You can call that function anywhere in your code and only really need to change the platform specific implementation that exists in the function definition. Any time you need to port the code you simply go to the library file, comment out the code that is being changed, and uncomment the code you want to use for the new platform.

platform specific implementation

make system calls portable

If something is platform specific, then by definition it is non-portable.

Just skip the "cls", it takes away control from the user and can, IMO, become annoying. If you need that kind of control, use a portable library like pdcurses.

This thread is over 2 years old, by the way.

Edited 5 Years Ago by jonsca: n/a

To clear a terminal or console on any Unix/Linux system or other system with an ANSI, xterm, or DEC VT-100-compatible terminal, you can output a ^L (control-L) hex 0x0C (form-feed character) to stdout and that should do it. BTW, the system command to clear the screen on Linux systems is "clear", not "cls". Anyway, there isn't a simple one-line function that will do this on all systems, unfortunately!

According to system function discussion. For me system("pause") is the best way to leave window open for Windows users to see results. On my system I just get info in terminal that this command was not found and program finishes normally. If someone would use appreciated (according to sticky) getchar, or cin.get, I would have to specially input character to finish a program. According to topic system("cls") is in my opinion the best option for beginner programmer to clear screen without having portability issues (of course you can use some #ifdefs and conio/curses/...). On systems without cls it will just output non fatal error, that cls program was not found.
@rubberman on debian and gnome-terminal trying to output 0xc just produces newline. Yes, maybe it is form feed, but DEC VT 100 time is gone.

Edited 5 Years Ago by Zjarek: n/a

According to system function discussion. For me system("pause") is the best way to leave window open for Windows users to see results.

And you'd be wrong.

On my system I just get info in terminal that this command was not found and program finishes normally.

And this is why.

If someone would use appreciated (according to sticky) getchar, or cin.get, I would have to specially input character to finish a program.

So? The character is the ENTER key. What's the big deal?

According to topic system("cls") is in my opinion the best option for beginner programmer to clear screen without having portability issues (of course you can use some #ifdefs and conio/curses/...).

And you'd be wrong again for the exact same reason. You have major portability issues, not none as you state. And since you mentioned beginner programmers, you want to teach them "#ifdefs and conio/curses/..."? Get real!

On systems without cls it will just output non fatal error, that cls program was not found.

And this is acceptable? You've got to be just barely beyond beginner yourself to have this opinion...

If not and you are a professional, ask your boss if it's OK with him if you "output non fatal error, that cls program was not found" is acceptable in your project. I suspect we all know the answer, therefore it can't be the best solution.

On systems without cls it will just output non fatal error, that cls program was not found.

The system function isn't as much a portability issue as it is a security hole. Anyone with half a brain can replace the program you're calling into with a malicious program of the same name and your code will happily execute it.

This article has been dead for over six months. Start a new discussion instead.