Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear Screen using C++

Tags:

c++

I would like to clear the CMD screen. I have seen a few options. First is

system("cls");  

but I don't want to use system, cause then it makes it dependent on Windows. Same with the Unix version.

If I try

cout << string(22, '\n');

then my next line of text is at the bottom of the screen and I want it at the top. How can I clear the screen and get the text back to the top of the screen?

Thus say I have this:

cout << string(22, '\n');
cout << "************Question 1 *******" << endl;
cout << "WHO WAS THE FIRST BLACK PRESEDENT?" << endl;
cout << "(1) Obama" << endl;
cout << "(2) Bush" << endl;
cout << "(3) Jordan" << endl;
cin >> answer >> endl;

This will clear the screen then put mymenu at the bottom of the screen... How can I make it clear the screen and put the question/answers back up top of the screen?

like image 965
Glen Morse Avatar asked Jun 27 '13 06:06

Glen Morse


People also ask

What is Clrscr () in C?

There are several methods to clear the console or output screen and one of them is clrscr() function. It clears the screen as function invokes. It is declared in “conio. h” header file. There are some other methods too like system(“cls”) and system(“clear”) and these are declared in “stdlib.

Does Clrscr work in C?

It is not a part of standard C library. Most of the modern C compilers do not include it. You can alternatively use the system(“command”) function to clear the screen using the appropriate command for your OS. Replace “command” with the appropriate command for your OS, “clear” for GNU/Linux and “cls” for windows.

What is CLS in C?

In computing, CLS (for clear screen) is a command used by the command-line interpreters COMMAND.COM and cmd.exe on DOS, Digital Research FlexOS, IBM OS/2, Microsoft Windows and ReactOS operating systems to clear the screen or console window of commands and any output generated by them.

What can be used instead of Clrscr in C?

To replace clrscr() , you can use system("cls"); (available with #include<stdlib. h> ).


1 Answers

Try this: it works both on Linux and Windows.

cout << "\033[2J\033[1;1H";

This is a string of special characters that translate to clear the screen command.

You can enclose this in a function like e.g. clrscr() depending on your implementation.

like image 185
catzilla Avatar answered Oct 02 '22 17:10

catzilla