c++ - 如何在C++中输出欧元符号

标签 c++

我试图为一个计算出租车费的程序输出一个 Eurosign,然后将其转换为美元或欧元。 规范的一部分是我必须输出 Eurosign。我已经尝试过 Unicode 但我没有任何运气。任何帮助将不胜感激,谢谢。

这是我的代码:

void produceFareInOtherCurrency(){
//Constant variable for eurosign
const char EUROSIGN = 128;

//Selects currency
switch (currency)
{
//Euros
case 'E':
price = price * 1.1;
cout << "Cost in euros is: " << price << EUROSIGN << ".\n\n";
break;

//Dollars
case '$':
price = price * 1.22;
cout << "Cost in US Dollars is: $"<< price << ".\n\n";
break;

//Exit
case '#':
break;

//Error message
default:
cout << "ERROR: Currency unknown!\n\n";
}

}

最佳答案

Windows 对在控制台中打印 Unicode 的支持有限。在 Visual Studio 中,您可以使用 _setmode 打印 Unicode 符号:

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

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"€\n";
    return 0;
}

请注意,如果您打算再次使用 std::cout,则必须调用 _setmode(_fileno(stdout), _O_TEXT)

如果使用 MinGW,请改用以下内容:

#include "windows.h"

void myprint(const wchar_t* str)
{
    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wcslen(str), NULL, NULL);
}

int main()
{
    myprint(L"€\n");
    return 0;
}

在 Linux 中你可以简单地尝试

std::cout << "€\n";

关于c++ - 如何在C++中输出欧元符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40612344/

相关文章:

Python OpenCV - ConvexHull 错误 "Points is not a numpy array, neither a scalar"?

c++ - 如何使内存对齐类的派生类失去对齐

c++ - 在 Release模式下调试 shell 扩展

c++ - 模板实例化不 "do inheritance"

c++ - 从usb获取设备名称

c++ - 回车 c++\r 和\b 输出

c++ - boost phoenix 的 map new_?

c++ - 防止 DLL 文件通过 MS Detours 加载到我的进程中

c++ - QT 4.5 - QGraphicsItem 分配给 0x0 的父场景

c++ - 如何在 C++/Qt 中设置 30fps 循环