各位程序猿们们字符串里的中文无法输出是为什么?

问题遇到的现象和发生背景

求问为什么字符串中的中文没法打印出来(visaul stdio 2017)

问题相关代码,请勿粘贴截图

#include
#include
using namespace std;
int main ()
{
string test = "test1怎么不打印中文";

for (int i = 0; i < sizeof(test) / sizeof(test[0]); i++)
{

    if (test[i] == NULL) { break; }
    { cout << test[i] << endl;
    }

}
system("pause");
return 0;

}

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

因为一个中文是两个字节, cout << test[i] << endl;一个字节一个字节的循环输出
遇到中文时是每次循环半个中文的字节(一个字节)
cout << test[i] << endl;是每输出半个中文的字节(一个字节)之后 endl换行,自然不对了
把 endl换行去掉就可以了

你题目的解答代码如下:

int main ()
{
string test = "test1怎么不打印中文";
for (int i = 0; i < test.length(); i++)
{
    cout << test[i];
}
system("pause");
return 0;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img