|
#include<iostream>
using namespace std;
int main()
{
const int MAX{ 80 };
char buffer[MAX];
int count{0};
cout << "Enter a string of less than" << MAX << "characters:\n" ;
cin.getline(buffer, MAX, '\n');
while (buffer[count] != '0')
count++;
cout << endl
<< "The string \" "<< buffer
<< "\"has" << count << "characters";
return 0;
}
输入nihao count数字输出为60,好像不对,问题出在哪里,请教。
本帖最后由 Health 于 2020-3-15 22:48 编辑
字符串的结束符是:'\0',这个字符的ASCII码是0
所以,应该这样写:
while (buffer[count] != '\0')
或者:
while (buffer[count] != 0)
都是可以的
而 '0' 是字符0,他的ASCII码是48,不是0,所以不能这么判断。
|
上一篇: hotkey的使用下一篇: vs2013打不开sln文件吗?
|