John Sall
John Sall

Reputation: 1141

How to return to the beginning of the console in printing in C++?

if I have this code :

using namespace std;

int main() {

    int count = 0;

    while (true) {
        cout << "here: " << count++ << " again\r";  
    }

    return 0;
}

After it finishes, it will go back to the beginning of the line with carriage return.

Now assume I have more than one line like so :

    using namespace std;

int main() {

    int count = 0;
    int count2 = 3;
    int count3 = 4;
    

    while (true) {
        cout << "here: " << count++ << " again\r";
        cout << "here: " << count2++ << " again\r";
        cout << "here: " << count3++ << " again\r";
        cout << "here: " << count-- << " again\r";
    }
    

    return 0;
}

It's not working. I want to keep all lines, and when one of them finishes it returns at the beginning, but what I get is only one line in the console

Upvotes: 0

Views: 364

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136238

Positioning of text in a terminal is outside the scope of the C++ standard.

You can use portable ncurses library for positioning of text on terminal/console.

Upvotes: 6

Related Questions