0

I tried to print out "Text här", and it printed out "Text hôr". "ö" turns into "+" and "å" turns into "Ô".

Here is the whole code.

#include <iostream>

int main()
{
std::cout << "Text här";
return 0;
}

I use visual studio 2015. What could be causing this and how would I get it fixed?

Kimple
  • 117
  • 1
  • 9
  • 5
    Probably you can find an answer here: https://stackoverflow.com/questions/1371012/how-do-i-print-utf-8-from-c-console-application-on-windows – Stefano Balzarotti Nov 26 '16 at 12:22
  • The default configuration of a console on Windows to this day is still based on choices that were sensible back in 1982. Very hard to fix, too many business-critical apps assume it is still 1982. Just tell your text editor about it. In VS do so with File > Save As > arrow on the Save button > Code page 437. – Hans Passant Nov 26 '16 at 13:47
  • Title should actually be 'My program doesn't print the correct nordic characters' – stijn Nov 26 '16 at 13:47
  • @HansPassant: Lying to the compiler about the source encoding, is very bad advice. In so many ways. – Cheers and hth. - Alf Nov 26 '16 at 16:53

1 Answers1

2

It's just a mismatch between the character encoding used in your executable, and the one used in the console window.

You can change the console window's character encoding via the chcp command.

You can issue that manually or e.g. in your program:

system( "chcp 1252 >nul" );

To avoid most of the encoding issues and handle international characters in general, you can use Unicode i/o.

However, the C++ standard library's support is next to non-existent, which means using platform-specific functionality, and secondly, the console windows are essentially limited to the Basic Multilingual Plane of Unicode, corresponding to original 16-bit Unicode, because of the original API design.

In practice these issues, and others, mean that there's a difference between beginner's exploratory code, and professional portable code.


You may find the following useful: (How can I make Unicode iostream i/o work in both Windows and Unix-land?)

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331