Glen Morse
Glen Morse

Reputation: 2593

Clear Screen using 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?

Upvotes: 15

Views: 110633

Answers (5)

satan
satan

Reputation: 1

the clear was right test my self it is system("cls");

for the other it should be like this:

    #include<iostream>
    using namespace std;
    #include<iostream>
    #include <string>

    int main(){

    int df = 0;

    while(df < 4){

    string answer;


   if (answer == ""){
    cout << "*****Q*****";
    cout << "WHO WAS THE FIRST BLACK PRESEDENT?";
    cout << "(1) Obama";
    cout << "(2) Bush";
    cout << "(3) Jordan";
    cin >> answer;
    }

    if (answer == "1"){
   cout << "yes its Obama";
   cin >> answer;
   }

    if (answer == "2"){
    cout << "no its not bush";
    cin >> answer;
    }

   if (answer == "3"){
    cout << "no its not Jordan";
    cin >> answer;
    }

    }

    return 0;
    }

that makes the game

Upvotes: -1

catzilla
catzilla

Reputation: 1981

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.

Upvotes: 42

user1095108
user1095108

Reputation: 14603

Another way would be to use OpenGL, Qt or SDL, which are cross-platform and write a graphical console. This can be seen in many roguelike games, for example Dwarf Fortress.

Upvotes: 0

CmdrMoozy
CmdrMoozy

Reputation: 3941

If you want a solution that will work on Windows, Mac & Linux/UNIX, you will need to come up with your own implementation. I do not believe that there is a single way to do it that works on all platforms.

For Mac/Linux/UNIX/BSD/etc., ncurses provides an easy way to do this (http://www.gnu.org/software/ncurses/).

For Windows, you will probably want to look into conio.h (http://en.wikipedia.org/wiki/Conio.h) or PDCurses (http://pdcurses.sourceforge.net/) or something similar. Alternatively, it would seem that you can do this without any third-party libraries, according to this Microsoft KB article: http://support.microsoft.com/kb/99261.

There is unfortunately no standard C/C++ function to do this. You should be able to write a small function which will build & work on any platform using the different methods I mentioned and some preprocessor directives.

If you don't have a convenient way to detect the platform, I would probably recommend cmake.

Upvotes: 9

Aswin Murugesh
Aswin Murugesh

Reputation: 11080

In UNIX try

system("clear")

clrscr() may not work in UNIX because some compilers do not support conio.h

Upvotes: -1

Related Questions