Abluescarab
Abluescarab

Reputation: 547

Clearing the Console in C++

I am trying to clear the console in C++. I know printing a number of newlines is a bad practice, as it can be slow and is not always reliable to completely clear the console window, but I have researched multiple options and have found almost no other solutions besides system("cls"), which is an even worse option.

Essentially, I have used the line cout << string(100, '\n'); but I am getting a near-unidentifiable error when I try to run the program.

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

I have also researched this, and found that most explanations were too complicated for me as a beginning C++ programmer to understand, or completely unrelated to my problem.

My questions are (1) is there a way to fix this error, and (2) could there be a better, cross-platform way of clearing the console other than printing 100 newlines?

I also heard of Console.clear(), but I'm unsure if this is cross-platform. From what I've seen, it looks more like a Windows command. I've also heard of the curses library, which I was willing to research and use, until I read somewhere that it was not recommended to use the functions which I am familiar with coupled with the curses library functions.

Thank you in advance!

Upvotes: 3

Views: 6889

Answers (3)

Pawn
Pawn

Reputation: 21

I know this is a complete necro. But I figured out what I feel is a rather neat solution and thought I'd share it just in case someone has this problem in the future.

void clearConsole() {
#ifdef _WIN32
#include <iostream>
    static const char* CSI = "\33[";
    printf("%s%c%s%c", CSI, 'H', CSI, '2J');

#else
#include <unistd.h>
    write(1, "\E[H\E[2J", 7);
#endif
}

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490118

At a guess, your immediate problem is probably that you're missing an #include <string>.

Probably the most portable way of dealing with the screen is via ncurses. It's included in POSIX and most POSIX-like systems, and available as a library for most others (e.g., Windows) as well.

Edit: For what it's worth, clearing the screen on Windows doesn't require anywhere close to 100 lines of code.

#include <windows.h>

void clear_screen(char fill = ' ') { 
    COORD tl = {0,0};
    CONSOLE_SCREEN_BUFFER_INFO s;
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);   
    GetConsoleScreenBufferInfo(console, &s);
    DWORD written, cells = s.dwSize.X * s.dwSize.Y;
    FillConsoleOutputCharacter(console, fill, cells, tl, &written);
    FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
    SetConsoleCursorPosition(console, tl);
}

#ifdef TEST
int main(){ 
    clear_screen();
    return 0;
}
#endif

I'm the first to say that the code is more verbose than I'd like -- but it's less than ten lines, not to mention a hundred. Even the version in the MS knowledgebase is actually less than 40 lines -- of which many are blank or comments.

In fairness, however, I feel obliged to admit assembly language code writing directly to the hardware (or using the BIOS) does end up quite a bit shorter.

Upvotes: 3

Salvatore Previti
Salvatore Previti

Reputation: 9050

About your error... you have to...

#include <iostream> 
#include <string>

using namespace std;

If you are using just windows use windows console API. If you are using a linux\unix terminal, use escape codes. You can do a #if to choose between the two methods.

On linux\unix use the write function defined in in this way:

write(1,"\E[H\E[2J",7); // we use ANSI escape sequences here.

Here is the microsoft page that explain how to do that.

http://support.microsoft.com/kb/99261

The really bad console api microsoft use for the console always makes me angry :) why 100 lines of code to clear a screen? :)

Now the if... you should create a clearscreen.h file and a clearscreen.cpp file.

In clearscreen.h we just put our function.

 void clearconsole();

In clearscreen.cpp we put our code for both operative systems

#ifdef _WIN32 || _WIN64

    #include <windows.h>

    void clearconsole()
    {
        ...
        // 100 lines of codes copied from microsoft article
    }

#else

    #include <unistd.h>

    void clearconsole()
    {
        write(1,"\E[H\E[2J",7);
    }

#endif

Upvotes: 3

Related Questions