Error handling using ctype.h (C++)

166 Views Asked by At

I've discovered the usage of library in C++ which is

ctype.h

I have an user input which is a string to accept words and is doing error handling using ispunct() to not accept punctuations. But I want ispunct() to accept " ' ". Is there anyway for me to set the the parameter to skip " ' "?

3

There are 3 best solutions below

1
On

If I understand your question correctly, you want to have ispunct return false on a ' character. You can just write a custom wrapper to it if this is the case.

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

Which first checks to see if c is a '. If it is, it returns 0, otherwise it passes c to ispunct and returns from that.

0
On

No, there isn't, since '\'' is punctuation, and that's what ispunct() looks for. You can check the characters manually.

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

where word is your string.