Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Go back a line

Tags:

c++

c++11

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?

like image 355
Felipe Nascimento Avatar asked Jul 16 '17 23:07

Felipe Nascimento


1 Answers

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.

like image 142
Philipp Claßen Avatar answered Oct 08 '22 06:10

Philipp Claßen