Lee360TheCoder
Lee360TheCoder

Reputation: 37

How do you clear the screen in a console application written in C++?

This is my first post. How do you clear the screen in a console application written in C++? Please understand I don't want to use any extra preprocessors. Would have to do:

cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

Could I do that, is there a more professional way of doing this?

Upvotes: 0

Views: 7412

Answers (2)

Prabhat Maurya
Prabhat Maurya

Reputation: 26

too simple depending on your OS.

on linux use >

 system("clear");

on windows use >

system("cls");

for cross platform app you can use both together. It would not harm your program as such.

Upvotes: 1

const_ref
const_ref

Reputation: 4096

In pure C++ you cannot since C++ doesn't even have the concept of a console. You could essentially be printing to anything(file, printer, dashboard) or even redirecting to another program etc.

It therefore depends on the OS or relies on you using a Library such as ncurses

In windows, for example, you can do the following

#include <stdlib.h>

int main(int argc, char* argv[])
{
  system("cls");
  return 0;
}

Upvotes: 4

Related Questions