samvel1024
samvel1024

Reputation: 1163

C++ cleaning the console window

IS there a way to delete or rewrite a symbol on the console window? Something like System("cls") but for a single symbol.

Thanks

Upvotes: 0

Views: 325

Answers (1)

Lol4t0
Lol4t0

Reputation: 12547

First of all, you are usually working with standard output stream with C++, not with console itself. And you are not able to navigate through it.

From the other hand, you can use specific platform dependent console libraries (like ncurses or Windows console functions) to handle console.

However, if you are actually printing to the console you can use some symbols to control the last line:

  • You can send '\b' (Backspace) to move one character left on the current line.
  • You can send '\r' to move to the beginning of the current line.

Don't forget also, that stdout usually line-buffered and you may have to flush by hands.

int main()
{
    std::cout << "Hi\r" << std::flush;
    Sleep(1000); //or whatever to delay
    std::cout << "hellq" << std::flush; // flushing by hands
    Sleep(1000);
    std::cout << "\bo";
}

Upvotes: 3

Related Questions