我如何允许C++输入?
我一直在尝试通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论