我是新手,目前正在学习 C++。我目前正在学习 cctype(ctype) 中的字符函数。我无法理解为什么 isspace(a_character) 没有返回我的 cout 消息——问题是它甚至不接受我的 Char 用户输入。任何帮助或转向正确的方向将不胜感激。如果我分配 Char 值: ' ' ,我可以获得我想要的响应,但是,它达不到目的。我复制了一部分代码。据我了解,Whitespace 没有确切的符号?如果是这样,是否可以输入空格作为输入?我试过输入 : ' ' 但是,没有成功。再一次,我将不胜感激。
#include <iostream>
#include <cctype>
#include <ctype.h>
using namespace std;
int main()
{
char c ;
char ans = 'y' || 'Y';
do {
cout << "Enter a character \n";
cin >> c;
if (isspace (c)) //Having trouble get this to actually produce a value
{
cout << "Your character " << c << "is a whitespace";
}
if (ispunct(c))
{
cout << c << " is a punctuation character\n";
}
cout << "Would you like to enter another value? \n";
cin >> ans;
} while (ans == 'Y' || ans == 'y');
return 0;
}
最佳答案
>>>
是一个格式化的提取器。默认情况下,格式化提取器会在实际读取任何内容之前提取并丢弃所有空白字符。这是有用的行为,但在您尝试读取空白字符时不是这样。
使用无格式输入 (get()
) 或 noskipws
操纵器。请注意,您稍后阅读 ans
很可能取决于跳过空格才能正常工作,因此您可能希望使用 skipws 恢复跳过空格的行为
如果您选择第二个选项,请在阅读 c
之后。
关于c++ - Isspace 字符函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43383393/