如何读取符号,直到从 cin 中按下 ESC 按钮C++
How to read symbols until ESC button is pressed from cin in C++
我需要让我的while
循环工作,直到按下键盘上的ESC按钮。
char choose = NULL;
while( choose != 27)
{
cout << "Choose (s), (e) or (n): ";
cin.ignore();
choose = cin.get();
switch(choose){
case 's': {SortRoutesByStartPoint(routeList, n); ShowRoutes(routeList, n, "Sorted list (s):"); break;}
case 'e': {SortRoutesByEndPoint(routeList, n); ShowRoutes(routeList, n, "Sorted list (e):"); break;}
case 'n': {SortRoutesByNumber(routeList, n); ShowRoutes(routeList, n, "Sorted list (n):"); break;}
default: {cout << "Not foundnn"; break;}
}
}
但是当我按下ESC按钮时,它什么也没发生。为什么?
如何让它工作?
简单的答案是,你不能,至少不可靠,使用标准流。 如果您输入自终端,您可以获得操作系统为您提供的东西。 总的来说,它在按下回车键之前不会给您任何东西,并且去除大量字符以进行行编辑和其他内容在此之前;ESC是其中之一,这是一个明显的变化字符。
当您想逐个字符阅读时(显然您显然做),你需要一些第三方库,比如诅咒。 或者你会有自己编写大量依赖于系统的代码。
#include <conio.h>
cout << "Choose (s), (e) or (n): ";
// cin.ignore();
// choose = cin.get();
choose =getche();
if(choice==0) //an extended character code is next
choice=getche();
相关文章:
- C++中的cin.ignore()函数不适用于整个流
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- Problems with std::cin.fail()
- 由cin中的字符串中未捕获空白引起的分割错误
- 在C++中使用Cin,我如何在1行中输入
- 将 cin 限制为只有一个
- cin >> int 给定一个字符串将 int 赋值为 0
- istream std::cin如何修改自定义istream缓冲区
- C++ 将 CIN 值存储到任何类型的数组中
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- cin 的十进制输入验证?
- Turbo C++ cin() 不能与 gets() 一起使用
- 使用 cin 时接受小数点后的 2 位数字
- 在 std::getline 和 std::cin 期间卡在循环中
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 为什么我的两个 cin 语句没有在程序结束时运行?
- C++ cin.ignore() 跳过循环
- CIN是逻辑1还是0?
- 如何运行多行 cin?
- 从 cin 读取不会返回整个输入字符串