【发布时间】:2014-01-18 13:15:59
【问题描述】:
这可能之前被问过 100 次,但我找不到或提出解决方案。 一切(据我测试)都被识别,只有紧迫的空间不起作用。我正在输入一个空格+回车,光标只是移动到下一行,没有任何反应。我怎样才能做到这一点?
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, char** argv) {
cout << "Press a key please: " << endl;
char key;
cin >> key;
if (isalpha(key))
cout << key << " is a letter." << endl;
else if (isdigit(key))
cout << key << " is a number." << endl;
else if (isspace(key))
cout << key << " is a space." << endl;
else if (ispunct(key))
cout << key << " is a punctuation" << endl;
return 0;
}
【问题讨论】:
-
做一些调试。当你按下空格时,
key里面是什么? -
“这可能之前被问了 100 次” 是的,一个简单的搜索就会显示 this !
-
@DavidHeffernan
32 ' '。我找了空间的 ANSII 代码,它是 32,但不知道如何使用它。 -
这不是真的。
cin >> key没有在key中添加空格。 -
@P0W 我已经搜索过了。我能找到的唯一参考是如何用
isspace识别字符串中的空格。那是在cplusplus.com/reference/locale/isspace
标签: c++