PRIGORYAN
PRIGORYAN

Reputation: 23

Is that C++ compiler error?

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.

Compiler 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?

Upvotes: 0

Views: 213

Answers (1)

mweerden
mweerden

Reputation: 14051

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++?

However, as mentioned by someone in the above post, you shouldn't really be modifying the locale like that. Instead you should be working with whatever locale is set. To use this information, you can use:

std::setlocale(LC_ALL, "");

Upvotes: 2

Related Questions