5

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?

Felipe Nascimento
  • 207
  • 1
  • 4
  • 10

1 Answers1

8

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.

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239