C++: Printing ASCII Heart and Diamonds With Platform Independent

前端 未结 4 1248
孤城傲影
孤城傲影 2021-01-02 17:13

I\'m developing a card playing game and would like to print out the symbol for hearts, diamonds, spades and clubs. My target platform will be Linux.

In Windows, I k

4条回答
  •  执笔经年
    2021-01-02 17:26

    On Linux, you can almost always write UTF-8 to stdout and Unicode characters will be displayed beautifully.

    #include 
    
    const char heart[] = "\xe2\x99\xa5";
    
    int main() {
        std::cout << heart << '\n';
        return 0;
    }
    

    You can find UTF-8 encodings of Unicode characters on sites like fileformat.info (search that page for "UTF-8 (hex)").

    Another way is to use wide characters. You first need to call setlocale to set things up. Then just use wchar_t instead of char and wcout instead of cout.

    #include 
    #include 
    
    const wchar_t heart[] = L"\u2665";
    
    int main() {
        setlocale(LC_ALL, "");
        std::wcout << heart << L'\n';
        return 0;
    }
    

提交回复
热议问题