可编辑的控制台输出
这是我试图编写的一些代码的一部分:
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这:
这并不完全是您想要的,但它是通过最少的返工就能获得的最接近的结果。
This this:
This is not exactly what you want, but it's the closest one can get with minimal rework.