title: How can I allow Chinese characters in C++ input?
body: <p>I've been trying to take Chinese (simplified) inputs through both the <code>iostream</code> in Visual Studio 2019. When I input an English word, it works fine. To make sure that the console is printing correctly, I changed the console font to something compatible with Chinese characters. Printing characters doesn't appear to be much of an issue as of yet, but inputting characters is causing me some trouble.</p>
  <p>I tried a couple of 'fixes' found in other posts such as <code>setlocale(LC_ALL, &quot;zh_CN.UTF-8&quot;)</code> or <code>wcin.imbue(locale(&quot;zh_CN.UTF-8&quot;))</code> or <code>SetConsoleCP(UTF8)</code>, and variants of those functions. While trying to debug, I found that the value of <code>b = L&quot;&quot;</code>; the characters that I typed in were completely ignored.</p>
  <p>However, when I read input from the file, it appears to be functioning; I can parse, manipulate, and print files properly.</p>
  <pre class="lang-cpp prettyprint-override"><code>void prepare() {
      HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  
      // Change font to something compatible with chinese
      CONSOLE_FONT_INFOEX fontInfo;
      fontInfo.cbSize = sizeof(fontInfo);
      fontInfo.FontFamily = 54;
      fontInfo.FontWeight = 300;
      fontInfo.nFont = 0;
      const wchar_t myFont[] = L&quot;KaiTi&quot;;
      fontInfo.dwFontSize = { 9, 30 };
      copy(myFont, myFont + (sizeof(myFont) / sizeof(wchar_t)), fontInfo.FaceName);
  
      SetCurrentConsoleFontEx(hConsole, false, &amp;fontInfo);
  
      setlocale(LC_ALL, &quot;zh_CN.UTF-8&quot;);
  
      //wcin.imbue(locale(&quot;zh_CN.UTF-8&quot;));
      //wcout.imbue(locale(&quot;zh_CN.UTF-8&quot;));
  }
  </code></pre>
  <p>Simplified sample code:</p>
  <pre class="lang-cpp prettyprint-override"><code>#define _UNICODE
  
  int main() {
      prepare(); // Prepare console to output Chinese characters properly
  
      wcin.imbue(locale(&quot;zh_CN.UTF-8&quot;));
      wcout.imbue(locale(&quot;zh_CN.UTF-8&quot;));
  
      wstring a = L&quot;你好&quot;;
      wcout &lt;&lt; a; // Prints and displays properly
  
      wstring b;
      wcin &gt;&gt; b; // &quot;你好&quot;
      wcout &lt;&lt; b; // Nothing prints
  
      return 0;
  }
  </code></pre>
  <p>How can I fix this issue? Thanks!</p>