使用ctype.h(C++)处理错误

Error handling using ctype.h (C++)

本文关键字:处理 错误 C++ ctype 使用      更新时间:2023-10-16

我在C++中发现了库的用法,它是

ctype.h

我有一个用户输入,它是一个接受单词的字符串,并且正在使用ispunct()进行错误处理,以不接受标点符号。但我希望ispunct()接受"。我是否可以将参数设置为跳过"?

如果我正确理解您的问题,您希望ispunct'字符上返回false。如果是这种情况,您可以直接为它编写一个自定义包装器。

int myispunct(int c) {
    return c == ''' ? 0 : ispunct(c);
}

其首先检查c是否是'。如果是,它返回0,否则它将c传递给ispunct并从中返回。

不,没有,因为'''是标点符号,而这正是ispunct()所寻找的。您可以手动检查字符。

try
{    
    if ( std::ispunct(word,loc) && word != "'"  )
        throw string("Punctuations other then ' are not allowed!");
}
catch(string ex)
{
    //error handling
}

其中word是您的字符串。