首页 > 解决方案 > 我无法使用 C++ 在 Visual Studio 2019 中打印无穷大符号

问题描述

我正在尝试打印无穷大符号(∞),但我不断收到垃圾。我已经尝试了这里提到的所有内容,但没有任何效果。

我想要完成的是这个

modifies strength by 9 ∞

我试过了

printf ("%c", 236);
printf ("%c", 236u);

我得到

modifies strength by 9 ì

我试过了

printf("∞");

我得到

modifies strength by 9 ?

我试过这个

if ( paf->duration == -1 ){
    setlocale(LC_ALL, "en_US.UTF-8");
    wprintf(L"%lc\n", 8734);
    ch->printf("∞");

只是想看看我是否可以让 wprintf 打印它,但它完全忽略了 setlocale 和 wprintf 并且仍然给了我

modifies strength by 9 ?

我试过了

if ( paf->duration == -1 ){
    std::cout << "\u221E";
    ch->printf("∞&quot;);

但是得到了这个警告和错误

Error   C2664   'int _CrtDbgReportW(int,const wchar_t *,int,const wchar_t *,const wchar_t *,...)': cannot convert argument 5 from 'int' to 'const wchar_t *'    testROS1a   C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\malloc.h   164 
Warning C4566   character represented by universal-character-name '\u221E' cannot be represented in the current code page (1252)    testROS1a   C:\_Reign of Shadow\TEST\src\CPP\act_info.cpp   3724    

我无法做出正面或反面。我已经用尽了我的知识范围,所以有人知道如何做到这一点吗?

标签: c++visual-studiovisual-c++utf-8

解决方案


这在我的机器 Windows + 代码页 (1252) 上起到了作用,但不确定这有多普遍。我从来没有真正用过 unicode/localization 的东西。而且似乎总是还有一个问题。

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

const wchar_t infinity_symbol = 0x221E;

int main()
{
    // enable windows console to unicode
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << infinity_symbol;
}

推荐阅读