3

I want to print extended ascii characters in console, I tried this:

    std::cout << "╔══════════╦════════════════════════════════════════════════════════╗\n";
    std::cout << "║  ID:678  ║  Name: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓        ║\n";
    std::cout << "║          ║  Surname: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ║\n";
    std::cout << "║          ║  Gender: ▓▓▓▓▓▓                                        ║\n";
    std::cout << "║          ║  Age: ▓▓▓                                              ║\n";
    std::cout << "║          ║  Tel: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓                  ║\n";
    std::cout << "║          ║  Adress: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ║\n";
    std::cout << "║          ║  ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ║\n";
    std::cout << "║ ID Print ║  Hobbies: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ║\n";
    std::cout << "║          ║  ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ║\n";
    std::cout << "║  ░░░░░░  ║  Additional İnformations: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ║\n";
    std::cout << "║  ▒▓▓▓▓▒  ║  ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ║\n";
    std::cout << "║  ▓▓▓▓▓▓  ║  ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓     ║\n";
    std::cout << "╚══════════╩════════════════════════════════════════════════════════╝\n";

but output is like this:

?????????????????????????????????????????????????????????????????????
?  ID:678  ?  Name: ????????????????????????????????????????        ?
?          ?  Surname: ????????????????????????????????????????     ?
?          ?  Gender: ??????                                        ?
?          ?  Age: ???                                              ?
?          ?  Tel: ???????????????????????????????                  ?
?          ?  Adress: ?????????????????????????????????????????     ?
?          ?  ?????????????????????????????????????????????????     ?
? ID Print ?  Hobbies: ????????????????????????????????????????     ?
?          ?  ?????????????????????????????????????????????????     ?
?  ??????  ?  Additional ¦nformations: ????????????????????????     ?
?  ??????  ?  ?????????????????????????????????????????????????     ?
?  ??????  ?  ?????????????????????????????????????????????????     ?
?????????????????????????????????????????????????????????????????????

But when i try this: std::cout << char(200); it successfully print '╔' character. so i'm sure Console supports extended ascii characters.

So; how can i output these cout's correctly?

Edit: using std::wcout << L""; isn't working.

Edit2: Program will only run in Windows 7 or later.

Edit3:

Using this code prints card out,

#include <fcntl.h>
#include <io.h>
_setmode(_fileno(stdout), _O_U16TEXT);

But then, I can't print any normal cout characters, it gives error. (example error string: std::cout << "Hello!" )

  • 2
    those characters depends on the codepage. Use Unicode instead [Output unicode strings in Windows console app](https://stackoverflow.com/q/2492077/995714), [How do I print Unicode to the output console in C with Visual Studio?](https://stackoverflow.com/q/46512441/995714) – phuclv Sep 28 '20 at 08:03
  • It's relevant to include in your post the environment where you use the code. – anastaciu Sep 28 '20 at 08:09
  • @anastaciu Edit2, added. It's only windows. – Ahmet Yusuf Yatkın Sep 28 '20 at 08:11
  • 2
    @AhmetYusufYatkın, it's kind of implied when you use `L` prefix, but it's better to specify it, so well done, as for the last part of your post, you need to keep using `wcout`, `std::wcout << L"Hello!";` or reset the encoding when you want to use `cout` again `_setmode(_fileno(stdout), _O_TEXT);`. – anastaciu Sep 28 '20 at 08:36
  • @anastaciu thank you very much, I answered my own question while you write this. but your answer was excatly what i want. – Ahmet Yusuf Yatkın Sep 28 '20 at 08:40
  • 1
    Data point: worked on my machine (macOS), using UTF-8, without doing anything special. (My **vim** editor defaults to UFT-8 without BOM.) I don't think Windows Console works with UTF-8, but (as per Ahmet's findings & answer) is able to work with UTF-16 using wcout. There is probably a way to plumb up std::cout to convert UTF-8 to UTF-16 and forward the output to std::wcout, for convenience... but that'd be an experiment for another day. – Eljay Sep 28 '20 at 12:22

1 Answers1

1

For guys who wrote on windows, can apply this: source(_setmode Windows source)

Needed Libraries:

#include <fcntl.h>
#include <io.h>

To transform cout to Unicode:

_setmode(_fileno(stdout), _O_U16TEXT);

*Important Note: in this style, you always supposed to be use std::wcout << L""; for this. if your code includes std::cout in other lines, here is the solution:

use _setmode(_fileno(stdout), _O_TEXT); to set stdout back to default text style, when you don't need special characters to print.