程序没问题,运行出来但运行出来之有letters有值其他为0为什么?

来源:百度知道 编辑:UC知道 时间:2024/06/07 15:26:27
#include<iostream>
using namespace std;
int main()
{
char c;
int letters=0,space=0,digit=0,other=0;
cout<<"enter one line:"<<endl;
while((c=getchar())!='\n')
{
if(c='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
other++;
}
cout<<"letters:"<<letters<<",space:"<<space<<",digit:"<<digit<<",other:"<<other<<endl;
return 0;
}

#include<iostream>
using namespace std;
int main()
{
char c;
int letters=0,space=0,digit=0,other=0;
cout<<"enter one line:"<<endl;
while((c=getchar())!='\n')
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
other++;
}
cout<<"letters:"<<letters<<",space:"<<space<<",digit:"<<digit<<",other:"<<other<<endl;
return 0;
}

我觉得就是你的第一个判断条件应该加括号的。
试一试吧。

那要看你输入的什么了,如果你输入的全是字母,那当然只有letter有值了。