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
jbennet
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
>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?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
> 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.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
>On some operating systems and some devices, you can do just that.
Yes, but on the OP's operating system and device? Unlikely.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
No worries, Rash knows better than to get into a flame war with me. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
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?"
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
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!
rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
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.
Zjarek
Junior Poster in Training
79 posts since Oct 2009
Reputation Points: 20
Solved Threads: 18
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? Thecharacter 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, notnone 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 youare 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.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401