首页 新闻 会员 周边 捐助

控制台输入的中文无法正确显示

0
悬赏园豆:20 [已解决问题] 浏览: 32次 解决于 2025-03-04 18:11
#include <iostream>
#include <windows.h>

int main() {
    // 设置控制台输出字符编码为UTF-8
    SetConsoleOutputCP(65001);
    // 设置控制台输入字符编码为UTF-8
    SetConsoleCP(65001);
    std::cout << "你好,世界" << "hello, world" << std::endl;
    std::string name;
    std::cout << "输入你的名字" << std::endl;
    std::cin >> name;
    std::cout << "你好," << name << std::endl; 
    std::cin >> name;
    return 0;
}

上面的代码运行结果如下,我已经把控制台输入输出字符编码都设置为了utf-8,但是我给“name”输入“大地earth”时,最后只打印了“earth”

c++
next_world的主页 next_world | 初学一级 | 园豆:186
提问于:2025-03-04 00:41
< > 人人可用的开源BI工具
分享
最佳答案
0

收获园豆:20
echo_lovely | 小虾三级 |园豆:1648 | 2025-03-04 10:17
#include <iostream>
#include <windows.h>

int main() {
    // 设置控制台输出字符编码为UTF-8
    SetConsoleOutputCP(65001);
    // 设置控制台输入字符编码为UTF-8
    SetConsoleCP(65001);
    std::cout << "你好,世界" << "hello, world" << std::endl;
    std::string name;
    std::cout << "输入你的名字" << std::endl;
    std::getline(std::cin, name);  // 使用 getline 读取输入
    std::cout << "你好," << name << std::endl; 
    return 0;
}
echo_lovely | 园豆:1648 (小虾三级) | 2025-03-04 10:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册
Top