Is there a clear screen command in C++ because I need one and I have yet to find it.
Is there a clear screen command in C++ because I need one and I have yet to find it.
Do you mean in console?
system("cls");
That should do it.
★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
good lord...
Why is everything so much harder in C++???
I'm using dev-C++ the most recent version, and I'm making a standard windows program, how do I clear the screen without changing anything, only clearing what has been outputted to the screen
> Why is everything so much harder in C++???
Compared to what?
I dunno what you've been using, but you have to understand that C++ doesn't give a damn about your OS or the hardware on which it runs.
The word "screen" doesn't appear in the C++ standard, so consequently, there is no STANDARD way to "clear a screen". If (and I stress, IF) there is indeed a screen on your implementation, then how to clear is it part of the implementation.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Did you read the FAQ that Salem linked to?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ah, but the FAQ answer also presents a number of options. The first one seems pretty simple, assuming that you know the length of the screen: "Write newline characters until everything has scrolled off of the screen".
The fifth one (use system(), e.g., system("cls") or system("clear")) might be the one you are looking for, but of course it has its own disadvantages.
If all else fails, you could always go for the fourth option
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I don't understand why using system to clear a screen is remotely popular. Let's look at this example:
Now, since this program needs root, we set the setuid bit:Code:$ cd /tmp $ cat test.cpp #include <iostream> #include <cstdlib> int main() { // Because this program needs root setuid(0); // Show us all how good I am system("clear"); std::cout << "Look, mom, I cleared the screen!!!" << std::endl; }
Yes, it works as expected. Now...Code:$ make test g++ test.cpp -o test $ sudo chown root:root test $ sudo chmod 4755 test
This shows a bit on the security and portability of such a thing. And I wont even start talking about the speed...Code:$ cat clear #!/bin/bash head -n 1 /etc/shadow $ chmod 755 clear $ export PATH=/tmp:$PATH $ ./test root:!:13843:0:99999:7::: Look, mom, I cleared the screen!!!
If you have windows just copy/paste Option's 5 clear_screen function and you are done. Or inlcude conio.h and use clrscr() as noted int the FAQ (non-standard). If you have Linux use Option's 4 method.
P.S.: I would like an Option 7 in the FAQ that is like Option 6 but for Linux, even though it would require installing some library
I think I need a better understanding of how C++ before I figure this out can someone volunteer to add me to AIM and try and explain somewhat how to do this and why I have to do it this way?
EDIT: I have gotten it to work with
, but what is the purpose of the different methods? What are their uses, I'm sorry normally I'm a quick learner, but that FAQ confuses the **** outta meCode:system("cls");
Last edited by The7thCrest; 01-03-2009 at 01:27 AM.
Well each method has its advantages / disadvantages. I don't really see what more could be said about them.
Have you read the speed / security disadvantages of using system() (and not just system("cls") ) ?
I would go with the newline method. Just print a bunch of newlines and be done with it.
I don't quite understand the point on security, either. Even if someone places a "cls.exe" in the program's working directory, the system one should still take precedence, since we are not doing "./cls.exe"? (does Windows actually search the current directory before the rest of $PATH?
If the cls.exe in system32 (or where ever it is) is replaced... I think there is a bigger issue to worry about than how secure this C++ program is.
[edit]
IMHO the best option is to use "system("cls");", unless you want to clear the console 60 times per second or something like that.
Sure, it's not portable (and I'm a Linux guy), but just changing the "cls" to "clear" would make it work on UNIX. If he is just starting to learn C++, there are certainly more important things to spend time on than this little detail?
It's also much simpler than all other solutions.
So I say just make sure you know the limitations and shortcomings of this method, and use it anyways =).
[/edit]
Last edited by cyberfish; 01-03-2009 at 04:01 AM.