2

I've try this simple code to output polish characters using 'std::wstring' class. The class is constructed succesfully from wchar_t array but I don't know how to output it to the screen. That line "cout << X << endl ;" doesn't compile. Is it possible to output polish characters in console application written in native C++ ?. If so then how to work around this ?. Below is a simple code I did try to compile:

#include <iostream>
#include <conio.h>
#include <string>

int main(void)
{
    using namespace std ;
    const wchar_t data[] = {'ą', 'ę', 'ć'} ;
    wstring X(data) ;
    cout << X << endl ;
    getch() ;
    return 0 ;
}
user1978386
  • 237
  • 8
  • 19

1 Answers1

2

Use std::wcout instead of cout

After using wcout you should no longer use cout in your program. The first time you cout or wcout it sets the orientation of stdout for the duration of your program.

Salgar
  • 7,687
  • 1
  • 25
  • 39
  • if I use wcout then it says it's undeclared and if I use std::wcout then it says that wcout is not a member of std. I use Dev-C++ v.4.9.9.2 – user1978386 Apr 26 '13 at 12:25
  • Look at the top answer here. http://stackoverflow.com/questions/15581569/c-gives-an-error – Salgar Apr 26 '13 at 12:33
  • @Salgar Using `std::cout` or `std::wcout` should not affect the orientation of stdout, and the orientation of stdout should be irrelevant to `cout` and `wcout`: the "orientation", here, is a restriction due to the fact that C uses the same `FILE` object for both wide and narrow character output. C++ has two different objects, each with its own buffer, and you should be able to use both in a single program. Of course, since they both send to the same device, you might have to insert some flushes. But if you cannot output to both, the implementation is broken. – James Kanze Apr 26 '13 at 12:44
  • @Salgar Note that not all implementations are necessarily conform, and some may behave as you describe, even if it's not conform. – James Kanze Apr 26 '13 at 12:58
  • I've uninstalled old version of Dev-C++(4.9.9.2) and installed probably newest (5.4.1). The above code compiles fine(after replacing cout statement with wcout) but it doesn't output those polish characters. I'm using WinXP 64-bit SP2. What's going on ? – user1978386 Apr 26 '13 at 13:00
  • Does it has something to do with the fact that there is no Polish version of WinXp 64 ?. I've got english version of OS – user1978386 Apr 26 '13 at 13:04
  • @user1978386 _Displaying_ characters involves a lot of elements. Assuming you're putting the right characters into `wcout`: `wcout` translates them to character output using elements of the locale---if you haven't set the locale, it uses `"C"`, which generally will know nothing of anything other than the basic character set. You need a locale which will translate to something your target will understand. And once the target gets the character, it has to map it to the glyph in its font. Supposing there is one. (For Windows, this depends on the codepage.) – James Kanze Apr 26 '13 at 13:11
  • @user1978386 Some additional info about the locale. Under Unix, this is simple: the locale names have been standardized for a long time, and `"en_US.utf8"` should do the trick. I "think" that the most recent Windows also support this naming convention, but if so, it is very recent. Something like "English_US.65001" _might_ work (but utf-8 support in Windows is also very recent, and sometimes buggy). Or if all you're concerned about is Polish, `"Polish_Poland.1250"`. And in the console window, set the code page to the final number in the locale (command `chcp`). – James Kanze Apr 26 '13 at 13:21
  • By calling 'chcp' command in windows console it prints that active code page is 852 and I can write polish characters in that console window. However I couldn't get the above code working, but I found other way to print polish characters. I'd #include , then made function call: setlocale(LC_ALL, "Polish_Poland.852") ; and I can correctly output characters stored an 'char' array using operator<< throught std::wcout but not std::cout – user1978386 Apr 27 '13 at 15:06