1

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?

Ankur Shah
  • 125
  • 12
  • I don't know how C wide strings and related functions work but as far as I can tell from this test using wide strings is your issue. Normal chars work fine. https://wandbox.org/permlink/0pTATPFLXd28DnWs – Parsa Jul 19 '19 at 16:55

2 Answers2

2

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

Oblivion
  • 7,176
  • 2
  • 14
  • 33
0

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;
}
daxim
  • 39,270
  • 4
  • 65
  • 132
  • Thanks for the response. So I already had the keyboard setting to English U.K. and was able to display the U.K. pound symbol. Would setlocal(LC_ALL, “”) just get the local user settings? – Ankur Shah Jul 19 '19 at 07:35
  • I don't know. [Open a new question](https://stackoverflow.com/questions/ask) to get an answer. – daxim Jul 19 '19 at 08:55