我如何允许C++输入?

发布于 2025-01-22 21:33:29 字数 1562 浏览 4 评论 0原文

我一直在尝试通过Visual Studio 2019中的iostream进行中文(简化)输入。当我输入英语单词时,它可以正常工作。为了确保控制台打印正确,我将控制台字体更改为与汉字兼容的内容。到目前为止,打印字符似乎并不是很多问题,但是输入字符会给我带来麻烦。

我尝试了在其他帖子中找到的几个“修复程序”,例如setLocale(lc_all,“ zh_cn.utf-8”)或 wcin.imbue(locale(zh_cn.utf-8“”) ))或setConsolecp(utf8),以及这些功能的变体。在尝试调试时,我发现b = l“”的值;我输入的字符完全忽略了。

但是,当我从文件中读取输入时,它似乎在起作用。我可以正确解析,操纵和打印文件。

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"KaiTi";
    fontInfo.dwFontSize = { 9, 30 };
    copy(myFont, myFont + (sizeof(myFont) / sizeof(wchar_t)), fontInfo.FaceName);

    SetCurrentConsoleFontEx(hConsole, false, &fontInfo);

    setlocale(LC_ALL, "zh_CN.UTF-8");

    //wcin.imbue(locale("zh_CN.UTF-8"));
    //wcout.imbue(locale("zh_CN.UTF-8"));
}

简化的示例代码:

#define _UNICODE

int main() {
    prepare(); // Prepare console to output Chinese characters properly

    wcin.imbue(locale("zh_CN.UTF-8"));
    wcout.imbue(locale("zh_CN.UTF-8"));

    wstring a = L"你好";
    wcout << a; // Prints and displays properly

    wstring b;
    wcin >> b; // "你好"
    wcout << b; // Nothing prints

    return 0;
}

如何解决此问题?谢谢!

I've been trying to take Chinese (simplified) inputs through both the iostream 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.

I tried a couple of 'fixes' found in other posts such as setlocale(LC_ALL, "zh_CN.UTF-8") or wcin.imbue(locale("zh_CN.UTF-8")) or SetConsoleCP(UTF8), and variants of those functions. While trying to debug, I found that the value of b = L""; the characters that I typed in were completely ignored.

However, when I read input from the file, it appears to be functioning; I can parse, manipulate, and print files properly.

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"KaiTi";
    fontInfo.dwFontSize = { 9, 30 };
    copy(myFont, myFont + (sizeof(myFont) / sizeof(wchar_t)), fontInfo.FaceName);

    SetCurrentConsoleFontEx(hConsole, false, &fontInfo);

    setlocale(LC_ALL, "zh_CN.UTF-8");

    //wcin.imbue(locale("zh_CN.UTF-8"));
    //wcout.imbue(locale("zh_CN.UTF-8"));
}

Simplified sample code:

#define _UNICODE

int main() {
    prepare(); // Prepare console to output Chinese characters properly

    wcin.imbue(locale("zh_CN.UTF-8"));
    wcout.imbue(locale("zh_CN.UTF-8"));

    wstring a = L"你好";
    wcout << a; // Prints and displays properly

    wstring b;
    wcin >> b; // "你好"
    wcout << b; // Nothing prints

    return 0;
}

How can I fix this issue? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文