空间字符函数

Isspace Character Function

本文关键字:函数 字符 空间      更新时间:2023-10-16

我是新手,目前正在学习C++。我目前正在学习cctype(ctype(中的字符函数。我很难理解为什么isspace(a_character(不返回我的cout消息 - 问题是它甚至不接受我的Char用户输入。任何帮助或转向正确的方向将不胜感激。如果我分配 Char 值:",我可以实现我想要的响应,但是,它违背了目的。我已经复制了部分代码。据我了解,空白没有确切的符号?如果是这样,是否可以输入空格作为输入?我尝试输入:"但是,没有成功。再次,我将不胜感激。

#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 charactern";
      }
      cout << "Would you like to enter another value? n";
      cin >> ans;
   } while (ans == 'Y' || ans == 'y');
   return 0;
}
>>是一个

格式化的提取器。默认情况下,格式化提取器在实际读取任何内容之前提取并丢弃所有空格字符。这是有用的行为,但当您尝试读取空格字符时则不然。

使用无格式输入(get()(或noskipws操纵器。请注意,您以后对ans的读取很可能取决于跳过空格才能正常工作,因此,如果选择第二个选项,则可能需要在读取c后使用 skipws 恢复跳过空格的行为。