Visual C 以前的文本在终端中删除并替换为新文本

visual C++ Previous text erased and replaced by new text in terminal

本文关键字:文本 替换 新文本 删除 终端 Visual      更新时间:2023-10-16

这是我的代码。

#include <iostream>
#include <string>
#include <windows.h>
#include <iomanip>
using namespace std;
int main(){
char choice;
do{
    double x;
    double y;
    char z;
    double sum;
    cout << "Pick a operation + - * /" << endl;
    cin >> z;
    cout << "Enter a number" << endl;
    cin >> x;
    cout << "Enter another number" << endl;
    cin >> y;
    if (z == '+'){
        sum = x + y;
        cout << "Answer: " << sum << endl;
    }
    else if (z == '-'){
        sum = x - y;
        cout << "Answer: " << sum << endl;
    }
    else if (z == '-'){
        sum = x - y;
        cout << "Answer: " << sum << endl;
    }
    else if (z == '*'){
        sum = x * y;
        cout << "Answer: " << sum << endl;
    }
    else if (z == '/'){
        sum = x / y;
        cout << "Answer: " << sum << endl;
    }
    cout << "Would you like to enter another equation? (y/n)" << endl;
    cin >> choice;
} while (choice != 'n');
if (choice == 'n'){
    return 0;
}

system("PAUSE");
return 0;
}

我要做的就是做到这一点,以便替换当前行,而不是每个新的cout转到另一行。基本上,除了cin外,所有将发生在一行中,该行将出现在cout下方的行中。我知道我可以使用r,但是cin将尝试覆盖cout。我希望删除cin输入,然后cout将替换以前的cout

我是一个总的初学者,所以我真的不知道该怎么办。

现代控制台模拟器足够快,您可以清除屏幕并简单地重新打印要保留的文本,而不会引人注目。如前所述,您也可以使用诅咒库。

您知道这一点,但是:当您打印运输返回字符 r时,许多终端都会这样做。从历史上看,当电视具有物理打字机的打印轮和车厢时,这将使马车返回左位并允许覆盖。在某些机器(包括经典的Macintoshes(上,这将有其他作用,但是它将在当今大多数游戏机上使用,包括Windows cmd.exe,OS X和Linux。

虽然有这样做的方法,但它们都不是标准的。此链接显示ANSI逃生序列,但是每个终端都不同,请记住文档是您的朋友。