C/C++ printing UK pound symbol from wint_t

耗尽温柔 提交于 2021-01-01 06:34:57

问题


I am on a Linux system and set the keyboard setting to UK in order to capture and print out a UK pound symbol (£).

Here is my code:

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main ()
{
    wint_t wc;
    fputws (L"Enter text:\n", stdout);

    setlocale(LC_ALL, "");
    do
    {
        wc=getwchar();
        wprintf(L"wc = %lc %d 0x%x\n", wc, wc, wc);
    } while (wc != -1);

    return 0;
}

Also, I wanted to store the UK pound symbol (£) as part of a string. I've found that std::string does NOT indicate an accurate size when wide characters are stored...is wstring much better to use in this case? Does it provide a more accurate size?


回答1:


You can use std::put_money

#include <iostream>
#include <sstream>
// Include the std::put_money and other utilities
#include <iomanip>

int main(int argc, char **argv)
{
    // Value in cents!
    const int basepay = 10000;
    std::stringstream ss;

    // Sets the local configuration
    ss.imbue(std::locale("en_GB.utf8"));
    ss << std::showbase << std::put_money(basepay);

    std::cout << std::locale("en_GB.utf8").name() << ": " << ss.str() << '\n';

    return 0;
}

Live




回答2:


Set the locale first before getting the input.

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main () {
    setlocale(LC_ALL, "en_GB.UTF-8");
    wchar_t wc;
    fputws (L"Enter text:\n", stdout);
    do {
        wc = getwchar();
        wprintf(L"wc = %lc %d 0x%x\n", wc, wc, wc);
    } while (wc != -1);
    return 0;
}


来源:https://stackoverflow.com/questions/57101699/c-c-printing-uk-pound-symbol-from-wint-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!