可编辑的控制台输出

发布于 2024-12-06 17:49:30 字数 1443 浏览 0 评论 0原文

这是我试图编写的一些代码的一部分:

//Choice Based Menu
#include <iostream.h>
#include <conio.h>
int main()
{
    char choice;
    cout<<"Menu"<<endl<<endl;
    cout<<"A. Option A"<<endl;
    cout<<"B. Option B"<<endl;
    cout<<"C. Option C"<<endl;
    cout<<"Q. Quit"<<endl;
    cout<<endl<<"Choice:\t";
    do
    {
        choice=getch();
        cout<<choice<<"\r";
        switch(choice)
        {
            case 'A':
            {
                cout<<endl<<"Option A!";
                break;
            }
            case 'B':
            {
                cout<<endl<<"Option B!";
                break;
            }
            case 'C':
            {
                cout<<endl<<"Option C!";
                break;
            }
            case 'Q':
            {
                return 0;
            }
            default:
            {
                cout<<endl<<"Invalid Choice! Please try again.";
                break;
            }
        }

    }while(1);
}

由于循环无限期地继续,因此它在执行先前选择的选项的代码后等待另一个输入选项。

Menu

A. Option A
B. Option B
C. Option C
Q. Quit

Choice: A
Option A!

我希望“选择:A”行每次都更新为最近输入的选项。我希望将先前选择的选项(选项 A!)的输出替换为新选择的选项的输出。

您可能已经注意到,我尝试使用“\r”。这不起作用,因为它给了我一个回车符,即它移回到行的开头。我希望它仅向后移动一个字符,而不是移动到行的开头。

Here is a portion of some code I am trying to write:

//Choice Based Menu
#include <iostream.h>
#include <conio.h>
int main()
{
    char choice;
    cout<<"Menu"<<endl<<endl;
    cout<<"A. Option A"<<endl;
    cout<<"B. Option B"<<endl;
    cout<<"C. Option C"<<endl;
    cout<<"Q. Quit"<<endl;
    cout<<endl<<"Choice:\t";
    do
    {
        choice=getch();
        cout<<choice<<"\r";
        switch(choice)
        {
            case 'A':
            {
                cout<<endl<<"Option A!";
                break;
            }
            case 'B':
            {
                cout<<endl<<"Option B!";
                break;
            }
            case 'C':
            {
                cout<<endl<<"Option C!";
                break;
            }
            case 'Q':
            {
                return 0;
            }
            default:
            {
                cout<<endl<<"Invalid Choice! Please try again.";
                break;
            }
        }

    }while(1);
}

Since the loop continues indefinitely, it waits for another input option after executing the code of the previously chosen option.

Menu

A. Option A
B. Option B
C. Option C
Q. Quit

Choice: A
Option A!

I want the line "Choice: A" to update with the most recently entered option every single time. And I want the output of the previously selected option (Option A!) to be replaced with the output from a newly chosen option.

I tried using '\r' as you may have noticed. That does not work because it gives me a carriage return i.e. it moves back to the beginning of the line. I want it to move back only by one character, and not to the beginning of the line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-12-13 17:49:30

这:

#include <iostream.h>
#include <conio.h>
int main()
{
    char choice;
    cout<<"Menu"<<endl<<endl;
    cout<<"A. Option A"<<endl;
    cout<<"B. Option B"<<endl;
    cout<<"C. Option C"<<endl;
    cout<<"Q. Quit"<<endl;
    do
    {
        choice=getch();
        cout << "\r" << "Choice:\t"; // Moved into the loop
        switch(choice)
        {
            case 'A':
            {
                cout << "Option A!"; // No more endl
                break;
            }
            case 'B':
            {
                cout << "Option B!";
                break;
            }
            case 'C':
            {
                cout << "Option C!";
                break;
            }
            case 'Q':
            {
                return 0;
            }
            default:
            {
                cout << "Invalid Choice! Please try again.";
                break;
            }
        }
    }while(1);
    cout << endl; // New sole endl
}

这并不完全是您想要的,但它是通过最少的返工就能获得的最接近的结果。

This this:

#include <iostream.h>
#include <conio.h>
int main()
{
    char choice;
    cout<<"Menu"<<endl<<endl;
    cout<<"A. Option A"<<endl;
    cout<<"B. Option B"<<endl;
    cout<<"C. Option C"<<endl;
    cout<<"Q. Quit"<<endl;
    do
    {
        choice=getch();
        cout << "\r" << "Choice:\t"; // Moved into the loop
        switch(choice)
        {
            case 'A':
            {
                cout << "Option A!"; // No more endl
                break;
            }
            case 'B':
            {
                cout << "Option B!";
                break;
            }
            case 'C':
            {
                cout << "Option C!";
                break;
            }
            case 'Q':
            {
                return 0;
            }
            default:
            {
                cout << "Invalid Choice! Please try again.";
                break;
            }
        }
    }while(1);
    cout << endl; // New sole endl
}

This is not exactly what you want, but it's the closest one can get with minimal rework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文