PRIGORYAN PRIGORYAN - 3 months ago 10
C++ Question

Is that compiler error or I'm stupid?

I got some troubles trying to execute following code:

#include <iostream>
#include <regex>

int main(int argc, char** argv) {

std::wstring buffer; // Buffer string for input

std::wregex integerRegex(L"^-?[0-9]+$"); // Regex for integers (123, -123, etc.)

while (true) {

std::wcout << L"Enter your value:\n";
std::wcin >> buffer; // Input string from keyboard to determinate is it integer or not

// Check if integer or not
if (regex_match(buffer, integerRegex)) {

std::wcout << L"Integer!\n";

} else {

std::wcout << L"Unknown :(\n";

}

}

return 0;

}


This code should output "Integer!" if entered sequence is integer or "Unknown :(" if not. But in some case I got false-positive results:
When I enter something like: "-234а", where 'а' is cyrillic character - the code above say's it's integer, but it's not. Other cyrillic characters are not making such troubles.

Compiller is TDM-GCC 5.1.0
Compiled with following flags:

-std=c++11 -w -Wall -Wextra -pedantic -Werror -pg -pipe


Can someone explain what is the root of problem and who's wrong?

Answer

It seems wcin is trying to read the input as ASCII. The non-ASCII characters cause it to get into an exception state. Adding something like the following should solve it:

std::setlocale(LC_ALL, "C.UTF-8");

Or on Windows:

SetConsoleCP(CP_UTF8);

Here is some more information: What most correct way to set the encoding in C++?