c++输入一行字符,分别统计出其中英文字母,数字和其他字符的个数
当我随便输入一行如ssss只显示有3个字母或888ssss只显示2个数字3个字母这是怎么回事?voidtask05(){intl=0,n=0,o=0;charword;c...
当我随便输入一行如ssss只显示有3个字母或888ssss只显示2个数字3个字母这是怎么回事?
void task05() {
int l = 0, n = 0, o = 0;
char word;
cin.get(word);
while (word != '\n') {
cin.get(word);
if ((word >= 'a'&&word <= 'z') || (word >= 'A'&&word <= 'Z')) {
l++;
}
else if (word >= '0'&&word <= '9') {
n++;
}
else {
o++;
}
}
cout << "zimu" << l << endl << "shuzi" << n << endl << "qita" << o << endl;
} 展开
void task05() {
int l = 0, n = 0, o = 0;
char word;
cin.get(word);
while (word != '\n') {
cin.get(word);
if ((word >= 'a'&&word <= 'z') || (word >= 'A'&&word <= 'Z')) {
l++;
}
else if (word >= '0'&&word <= '9') {
n++;
}
else {
o++;
}
}
cout << "zimu" << l << endl << "shuzi" << n << endl << "qita" << o << endl;
} 展开
3个回答
展开全部
问题出在while循环里的cin.get()位置不合适,换成如下即可:
#include <iostream>
using namespace std;
int main()
{
int l = 0, n = 0, o = 0;
char word;
cin.get(word);
while (word != '\n') {
if ((word >= 'a'&&word <= 'z') || (word >= 'A'&&word <= 'Z')) {
l++;
}
else if (word >= '0'&&word <= '9') {
n++;
}
else {
o++;
}
cin.get(word);
}
cout << "zimu" << l << endl << "shuzi" << n << endl << "qita" << o << endl;
}
VS2017测试通过。答题不易,正确请采纳
追问
谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询