3

I have a short C++ program to calculate the wind-chill index given a temperature and wind speed. It's working fine on a Windows 10 machine and outputing exactly as it should. To print the degrees symbol ° I'm using static_cast<unsigned char>(248).
However, I'm getting different results on a MAC. I assume this is because of the different character set encoding between each of the two machines. How can I get around this problem, to ensure the degrees-symbol ° will print on both?

Here is the line of code and screen print of the output

cout << "When temperature is " << temp << " degrees and wind speed is " << windSpeed 
     << " m/s, then the wind chill is " << tempEff << static_cast<unsigned char>(248) <<"C\n\n";

Output on Windows: enter image description here Output on Windows

However, the same code returns a "\370" on a Macbook/MAC OS, which is incorrect. See output here:

enter image description here Output on Mac OS

nobody
  • 19,814
  • 17
  • 56
  • 77
JMBaker
  • 502
  • 3
  • 7
  • Weird thing for me is that `static_cast(248)>` prints the appropriate thing on my Windows machine but just trying to print `'°'` doesn't. – Nathan Pierson Nov 14 '21 at 19:38
  • 2
    `\370` is 248 in base 8, octal. Degree is in Unicode U+00B0, **`\u00b0`**, decimal 176. The encoding of the command line on Windows can be different from the Windows encoding in the editor. It is a mess. – Joop Eggen Nov 14 '21 at 19:39
  • 1
    248 is not the correct code for the degrees character. [This](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/) is required reading. – n. m. could be an AI Nov 14 '21 at 20:02

0 Answers0