What is the recommended way of reading some user input that can have special characters with e.g., accents, if it is not known in which locale
it is input.
How to safely compare a character of this user input if it is a special one, that I need to handle some way?
This is a sample code to illustrate the intent:
#include <iostream>
using namespace std;
int main() {
char txt[10];
cin.getline(txt, sizeof(txt));
if(txt[0] == 'á')
cout << "Special character found\n";
}
The problem is:
warning: multi-character character constant [-Wmultichar]
if(txt[0] == 'á')
^
If I use L'á'
as wide character literal, then it will not match, since the input is not wide.
If I use wchar_t
and wcin.getline
too to get user input in wide character, then it may work on some systems and may not on others, depending on the environment and the locale settings.
How to safely and portably get over this problem? Thanks!