#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
void Test(char h)
{
cout<<"press key===="<<h<<endl;
switch(h)
{
case 'a':
case 'A':
{
cout<<"123"<<endl;
break;
}
case '1':
{
cout<<"1234"<<endl;
break;
}
case 'q':
case 'Q':
{
exit(0);
break;
}
default:
{
cout<<"default "<<h<<endl;
break;
}
}
}
int main()
{
while(1)
{
Test(getch());
}
return 0;
}
getchar()有缓存;此程序有一点小bug,当按Enter键时,也输出了空" "=》"default " 。
getch()非缓存;
本文介绍了一个使用C++编写的简单程序,该程序通过getch()函数读取用户输入的字符并根据不同按键做出响应。文章展示了如何使用switch-case结构来实现对特定按键的响应,并解释了getch()和getchar()的区别。
1414

被折叠的 条评论
为什么被折叠?



