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

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

统一一下试试

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
#ifdef _WIN64
    system("chcp 65001");
#endif
    string a;
    cin >> a; // 假设输入: 中文
    cout << a << endl;
}

 

我复制你的可以输出

img


所以应该是你的软件编码不是utf-8