pokche pokche - 1 year ago 84
C++ Question

How to print from the beginning after endl in C++?

I am currently trying to print some thing on the console. Similar to loading in same line using "\r" but instead I have endl included.

#include <iostream>

int main(int argc, char **argv)
int x = 3;
// first part
std::cout << " x = " << x <<"\n";
std::cout << " y = " << x <<"\n";
std::cout << " z = " << x << "\n";

std::cin >> x ;
std::cout << "\r" << std::flush;
// second part
std::cout << " x = " << x <<"\n";
std::cout << " y = " << x <<"\n";
std::cout << " z = " << x <<"\n";

return 0;

The above code prints the first part and prints again the second part in different place. So the total column is eight. What I actually want is to print the first part take input from user and then replace the first part with second part. This way total column is 3.

Answer Source

You can write the backspace character '\b' to move backwards on your line. This only moves your cursor so you'll have to hen write your content after you have backed up.

To move up you can use '\e[A' and '\e[B' to move down.

Note: This solution is not very portable, but if it works for your situation go for it!

You may also want to look into system specific functions if you want to move multiple lines or clear the entire screen.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download