I'm writing a multiple lines system, like this:
string readLines(string x)
{
string temp = "a";
vector<string> lines(0);
string result;
while (1)
{
cout << x;
getline(cin, temp)
if(temp != "")
{
result = result + "\n" + temp;
lines.push_back(temp);
}
else
break;
}
return result;
}
Is working fine, but I want be able to edit the previous line, for example, I'm typing something like this:
Helo,
World
I want to back on helo
and fix my typo. How can I do this?
There is no portable way to go back one line in C++.
You can go to the beginning of the line by printing \r
, but moving to the previous line requires platform dependent code.
If don't want to use libraries like Curses, you can try ANSI escape codes. Depending on the terminal, cout << "\033[F"
will move the cursor one line up.
On Windows, there is also the SetConsoleCursorPosition API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With