5

For a console application, I need to display the symbol: √
When I try to simply output it using:
std::cout << '√' << std::endl; or
std::wcout << '√' << std::endl;,
it outputs the number 14846106 instead.

I've tried searching for an answer and have found several recommendations of the following:
std::cout << "\xFB" << std::endl; and
std::cout << (unsigned char)251 << std::endl;
which both display a superscript 1.

This is using the Windows console with Lucida font. I've tried this with various character pages and always get the same superscript 1. When I try to find its value through getchar() or cin, the symbol is converted into the capital letter V. I am, however, sure that it can display this character simply by pasting it in. Is there an easy way of displaying Unicode characters?

nwn
  • 583
  • 7
  • 23
  • 1
    14846106 == 0xe2 0x88 0x9a == utf-8 encoding of U+221a == '√'. Getting your text editor and your C++ compiler to agree about the source code file encoding is important to get ahead. – Hans Passant Aug 02 '13 at 01:15

1 Answers1

1

Actually "\xFB" or (unsigned char)251 are the same and do correspond to the root symbol √... but not in the Lucida font and other typefaces ASCII table , where it is an ¹ (superscript 1).

Switching to Unicode with the STL is a possibility, but I doubt it will run on Windows...

#include <iostream>
#include <locale.h>

int main() {
    std::locale::global(std::locale("en_US.UTF8"));
    std::wcout.imbue(std::locale());

    wchar_t root = L'√';

    std::wcout << root << std::endl;

    return 0;
}

Since this will not satisfy you, here a portable Unicode library: http://site.icu-project.org/

Kyle_the_hacker
  • 1,394
  • 1
  • 11
  • 27
  • Your provided code only yielded a runtime error, as expected. Thanks for the link, though. This should solve my problem. – nwn Aug 14 '13 at 00:22
  • 1
    The OP is using a codepage such as [CP 850](http://en.wikipedia.org/wiki/Code_page_850) in which `'\xFB'` is mapped to `U+00B9` (superscript 1), as opposed to the [CP 437](http://en.wikipedia.org/wiki/Code_page_437) mapping to `U+221A` (square root). You can of course print this character to the Windows console since it's within the Unicode BMP. Most TT fonts will have the glyph, such as Consolas. – Eryk Sun Nov 19 '14 at 17:01
  • 1
    UTF-8 isn't a valid locale encoding in Windows. See [this answer](http://stackoverflow.com/a/9051543) for the way to configure VC++ stdio to use UTF-16. If the source file is UTF-8 encoded, remember to save the file with a UTF-8 BOM (`"\xef\xbb\xbf"`) to let the compiler know. – Eryk Sun Nov 19 '14 at 17:34