Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does std::iswalpha return false for some French characters in C++?

I am using std::iswalpha to check if a non-ASCII character (French character) is alphabetic. However, I have found that it returns false for é character. I have set my locale to fr_FR.UTF-8 in my code. Can you help me understand why this is happening and how I can correctly determine whether a French character is alphabetic using C++?

#include <iostream>
#include <cwctype>

int main() {
    std::setlocale(LC_ALL, "fr_FR.UTF-8");
    wchar_t ch = L'é';
    bool is_alpha = std::iswalpha(ch);
    std::cout << is_alpha << std::endl; // print 0
    return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s because installing the locale fails, but you don’t check that.

Running this in Compiler Explorer prints locale: No such file or directory while running it on my local computer where I have the fr_FR.UTF-8 locale installed succeeds and prints 1:

#include <cstdio>
#include <cwctype>
#include <iostream>

int main() {
    if (std::setlocale(LC_ALL, "fr_FR.UTF-8") == nullptr) {
        std::perror("locale");
    } else {
        wint_t ch = L'é';
        bool is_alpha = std::iswalpha(ch);
        std::cout << is_alpha << std::endl;  // print 1
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading