Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

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
";  
    }

    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
";
        cout << "here: " << count2++ << " again
";
        cout << "here: " << count3++ << " again
";
        cout << "here: " << count-- << " again
";
    }
    

    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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...