我一直在尝试将回车键作为我程序的输入。我已经定义了一个 char ENTER 变量并使用了 cin >> ENTER;接受回车键。然后我使用 if 语句来确定是否按下了 Enter 键。 if(ENTER == '13'), '13'是回车的ascii码。它似乎不起作用,有什么建议吗?
最佳答案
如何在不破坏有效输入的情况下检测回车键:
char c;
cin.get(c); // get a single character
if (c == 10) return 0; // 10 = ascii linefeed (Enter Key) so exit
else cin.putback(c); // put the character back and proceed normally
或者:
char c;
c = cin.peek(); // read next character without extracting it
if (c == '\n') return 0; // linefeed (Enter Key) so exit
关于c++ - 您如何将回车键作为输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274228/