Constantine
Constantine

Reputation: 783

C++ error too big for character

Here is were i get the error. To explain, i want to print the → character which according to http://www.endmemo.com/unicode/unicodeconverter.php

The code is 2192. but i may be using the wrong code if so what is the right way to print → .

int _tmain(int argc, _TCHAR* argv[])
{
    UINT oldcp = GetConsoleOutputCP();
    SetConsoleOutputCP(CP_UTF8);

    cout<<"\x2192"<<endl;

    SetConsoleOutputCP(oldcp);

    return 0;
}

Upvotes: 1

Views: 1890

Answers (2)

Balog Pal
Balog Pal

Reputation: 17193

A char on your platform is 8 bits. Your code part "\x2192" tries to put 16 bits in it. What will not fit, so you get the warning.

You possibly meant several characters, like "\x21\x92" or "\x92\x21"? That creates a valid string with two chars (+ the 0). You may still adjust it to have the proper value if comments are correct.

Upvotes: 1

Carl Norum
Carl Norum

Reputation: 225112

From the use of _tmain and SetConsoleOutputCP I guess you are mostly about Windows. I'm afraid I don't know much about that; hopefully someone who knows more about that specific case will chime in, but this program generates the output you're looking for in a quick test I tried here with a UTF-8 terminal. Here's the program:

#include <iostream>

int main(void)
{
    std::cout << "\xE2\x86\x92" << std::endl;
    return 0;
}

And example output:

$ make example && ./example
c++     example.cpp   -o example
→

I just directly output the UTF-8 encoding of the → character.

Equivalently (at least for clang):

#include <iostream>

int main(void)
{
    std::cout << "→" << std::endl;
    return 0;
}

Upvotes: 0

Related Questions