"cls" command?

Hello, I was wondering if there was a c++ equivalent of a "cls" command? I'm running my code as a console application. I did some looking around, and the only thing that I could find was

system("cls")

It didn't work out for me, so could someone tell me if I did it wrong (very possible :P ) or if there is another way? Thanks! Below is an example of how I tried to use it:
(I also tried the "system("cls");" without the ";", no change :(


#include <iostream>

using namespace std;

int main()
{
cout << "Some sample text..." << endl;
system("cls");
return 0;
}



For me in Windows 7 std::system( "cls" ) works.
system(); is a function that is standard. However, the value inside of system relates to a batch file already on the computer. I'll do some digging, but there was a post about using the system function that explained it. As for the "cls" it's specific to windows. If you're using windows, open your command prompt and type in cls and see if it works for you.

I know there was a non standard clrscr(); function included in certain conio.h libraries, but I know it was definitely in the borland libraries. Otherwise, everyone is going to tell you that clearing the console should only be done by sending a bunch of line returns to the screen.

1
2
3
// 30 lines should be enough for a standard console window
for(int i = 0; i < 30; i ++)
   std::cout << "\n";


Edit: You need to include the cstdlib header #include <cstdlib> to use the system function, I believe.
Last edited on
Why do you need the console/terminal cleared? I can't see of any reason. When people open open up a terminal, I doubt they're expected it to get cleared. I would actually be annoyed if it got cleared, cause I just lost whatever info I had previously gotten.
Because of people like me. I hate a messy console. When you ask for a users input, on lets say a long menu, it makes the screen very crowded. I like clearing my screen. I don't encourage it because it's not standard, but I do use it myself.
That would be a situation where you don't use the console
Like I said, I don't encourage others to use it, just stating why I personally use it. What about when you're displaying some math on the screen that your program just did, and the user already entered the next equation in, should they be bothered by the previous equation? Just giving general scenarios. I don't think you should clear the screen at all, but I have specific uses for it that most people do not.
There are valid reasons to clear the screen.
http://cplusplus.com/articles/4z18T05o/
Make sure to read the caveats.
If you run windows this function clears the screen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void cls() //windows h function to replace screen with nulls
{
  DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
}


dont forget to #include <windows.h>
You need more sleep, dude. Why not click the link I posted? Why line 11? Where's your error handling?
I dont understand win32, but i have been using the function for ages with my programs, so far no errors. I was just trying to help out.... i got it from a friend and it does the job perfectly.
Topic archived. No new replies allowed.