Home > Blockchain >  C not printing emojis as expected
C not printing emojis as expected

Time:03-04

I have the following extract of a program

int main(){
  cout << "1) ✊\n";
  cout << "2) ✋\n";
  cout << "3) ✌️\n";

}

But at the time I run it I get strange texts like the following

====================
rock paper scissors!
====================
1) 
2) 
3) ԣÅ

This seems not to be related to my terminal but instead to a compilation result because if I run echo ✊ it shows as expected.

See example below console output for the program and echo program

I'm currently using the following compilation commands and compiler version g *.cpp -o rock_paper_scissors.exe

g .exe (Rev9, Built by MSYS2 project) 11.2.0 Copyright (C) 2021 Free Software Foundation, Inc.

Finally, note that it was working before as expected, but at some point, it stopped working, I noticed after I used system("pause") which I'm guessing may have changed something on the compilation configurations as this is a Windows-only command, I delete such piece of code and still having the issue.

You can see the rest of the code here: https://github.com/guillene/RockPaperScissors

CodePudding user response:

If your terminal font supports emojis and you don't want to write much code (like switching from cout to wcout), you can use the windows api function below.

#include <windows.h>
#include <iostream>

int main(){
  SetConsoleOutputCP(CP_UTF8);
  std::cout << "1) ✊\n";
  std::cout << "2) ✋\n";
  std::cout << "3) ✌️\n";
  return 0;
}

see: https://docs.microsoft.com/en-us/windows/console/setconsoleoutputcp

  • Related