C ++返回一行 [英] C++ Go back a line

查看:76
本文介绍了C ++返回一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个多行系统,如下所示:

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

我想回到 helo 并修正我的错字.我该怎么办?

I want to back on helo and fix my typo. How can I do this?

推荐答案

在C ++中没有可移植的方法返回上一行.

There is no portable way to go back one line in C++.

您可以通过打印 \ r 转到行的开头,但移至上一行则需要平台相关的代码.

You can go to the beginning of the line by printing \r, but moving to the previous line requires platform dependent code.

如果不想使用 Curses 之类的库,可以尝试 ANSI转义码.取决于终端, cout<<"\ 033 [F" 会将光标向上移动一行.

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.

在Windows上,还有 SetConsoleCursorPosition API.

On Windows, there is also the SetConsoleCursorPosition API.

这篇关于C ++返回一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆