求问为什么字符串中的中文没法打印出来(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;
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!