将键盘输入模拟传递给控制台应用程序
Pass keyboard input emulation to console application
我开发了简单的控制台菜单程序,该程序对键盘输入做出反应。
#include <iostream>
#include <cstdio>
using namespace std;
char* main_menu[] =
{
"1 - selection 1",
"2 - selection 2",
"3 - selection 3",
"4 - selection 4",
"q - quit",
NULL
};
char* menu2[] =
{
"a - selection a",
"b - selection b",
"c - selection c",
NULL
};
int getChoice( char* greet, char* choices[] )
{
int chosen = 0;
int selected;
char** option;
do
{
printf( "Choice: %sn", greet );
option = choices;
while (*option)
{
printf( "%sn", *option );
option++;
}
do
{
selected = getchar();
printf( "selected: %cn", selected );
} while ( selected=='n' );
option = choices;
while (*option)
{
if ( selected == *option[0] )
{
chosen = 1;
break;
}
option++;
}
if ( !chosen )
{
printf( "Incorrect choice, select againn" );
}
} while( !chosen );
return selected;
}
int getAmount( char* greet )
{
int amount = 0;
printf( "%sn", greet );
scanf("%d", &amount);
return amount;
}
int main(int argc, char **argv, char **envp)
{
int choice = 0;
do
{
choice = getChoice( "Please select an action", main_menu );
printf( "You have chosen: %cn", choice );
if ( choice == '1' )
{
printf( "choise 1");
}
else if ( choice == '2' )
{
printf( "choise 2");
}
else if ( choice == '3' )
{
int a = 0;
a = getAmount( "type int value" );
printf( "choise 3 entered %d",a);
}
else if ( choice == '4' )
{
int choice2 = 0;
choice2 = getChoice( "Please select an action", menu2 );
printf( "You have chosen: %cn", choice2 );
if ( choice2 == 'a' )
{
printf( "choise a");
}
else if ( choice2 == 'b' )
{
printf( "choise b");
}
else if ( choice2 == 'c' )
{
printf( "choise c");
}
}
} while ( choice != 'q' );
}
我想通过它传递密钥序列以对其进行测试。我在 bash 中运行命令并期望按键 1:
回声 1 |。/选择器
但是得到了垃圾字符的非连续循环:选择:请选择一个操作
1 - selection 1
2 - selection 2
3 - selection 3
4 - selection 4
q - quit
selected: �
Incorrect choice, select again
Choice: Please select an action
1 - selection 1
2 - selection 2
3 - selection 3
4 - selection 4
q - quit
selected: �
Incorrect choice, select again
Choice: Please select an action
我的命令或程序有什么问题?
问题是,您不检查文件结尾。
selected = getchar();
应该是这样的:
if ((selected = getchar())== EOF){exit(0);}
相关文章:
- C++控制台应用程序阻止退出
- 如何在 Win32 C++控制台应用程序中调用 UWP 类库
- 是否可以从控制台应用程序使用 C++/WinRT 创建窗口?
- 如何创建一个使用开源c项目的c++控制台应用程序
- c ++控制台应用程序,如何在控制台外部打印字符
- 将控制台应用程序直接输出到Visual Studio而不是外部Windows控制台中
- 如何在 Win32 控制台应用程序中注册不可见的窗口类?
- 如何将从控制台应用程序C++回调设置为C++ COM DLL
- 使用代码在 AWS 开发工具包C++控制台应用程序上设置凭证
- 直接输入控制台应用程序
- C++调试控制台应用程序
- C++控制台应用程序调用C++/CLI静态库
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- 如何在将Visual Studio C++控制台应用程序连接到Microsoft SQL Server时修复"
- 如何读取windows服务启动的控制台应用程序的输出
- VS2017 中缺少 Win32 控制台应用程序.如何创建C++空项目?
- 将 Visual C++ 6 控制台应用程序作为 Windows 服务运行
- 如何将控制台应用程序放入系统托盘通知C++?
- 如何向控制台应用程序添加图标
- 如何在 Linux/Apache 虚拟主机上运行 win32 控制台应用程序?