叮当over 2022-10-25 14:56 采纳率: 78.6%
浏览 211
已结题

C++ cin输入的汉字 无法使用cout输出

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a;
    cin >> a; // 假设输入: 中文
    cout << a << endl;
}

在vscode运行结果是 空的 和 一些符号 有时还是字母pp

img

img

但是在cmd运行是正常的

img

  • 写回答

3条回答 默认 最新

  • 关注

    编译编码和终端编码不一致, 一般都是windows下会出的问题.

    统一一下试试

    #include <iostream>
    #include <string>
    using namespace std;
     
    int main()
    {
    #ifdef _WIN64
        system("chcp 65001");
    #endif
        string a;
        cin >> a; // 假设输入: 中文
        cout << a << endl;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月4日
  • 已采纳回答 10月27日
  • 修改了问题 10月25日
  • 创建了问题 10月25日