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

时间:2013-08-25 17:33:23

标签: c++ ctype

我在C ++中发现了库的使用,这是

ctype.h

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

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

其中word是您的字符串。