Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows console does not display square root sign

Tags:

c++

windows

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?

like image 428
nwn Avatar asked Nov 12 '22 00:11

nwn


1 Answers

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/

like image 114
Kyle_the_hacker Avatar answered Nov 15 '22 05:11

Kyle_the_hacker