一汽奥迪a4:怎么才算跳出while循环,请高手指点一下

来源:百度文库 编辑:神马品牌网 时间:2024/04/25 15:47:25
#include<iostream>
using namespace std;
int main(){

char ch;
int acnt=0,ecnt=0,icnt=0,
ocnt=0,ucnt=0;
while(ch!='n'){
cin>>ch;
switch(ch){
case 'a':
acnt++;
break;
case 'e':
ecnt++;
break;
case 'i':
icnt++;
break;
case 'o':
ocnt++;
break;
case 'u':
ucnt++;
break;
}
}
cout<<"the number of a"<<acnt<<'\n'
<<"the number of e"<<ecnt<<'\n'
<<"the number of i"<<icnt<<'\n'
<<"the number of o"<<ocnt<<'\n'
<<"the number of u"<<ucnt<<endl;

}
要求输入一段文字统计元音字母个数,输入完文字如何跳出while循环啊

改成这样
#include<iostream>
using namespace std;
int main(){

char ch;
int acnt=0,ecnt=0,icnt=0,
ocnt=0,ucnt=0;yycnt=0;
while(ch!='n'){
cin>>ch;
switch(ch){
case 'a':
acnt++;
yycnt++;
break;
case 'e':
ecnt++;
yycnt++;
break;
case 'i':
icnt++;
yycnt++;
break;
case 'o':
ocnt++;
yycnt++;
break;
case 'u':
ucnt++;
yycnt++;
break;
}
}
cout<<"the number of a"<<acnt<<'\n'
<<"the number of e"<<ecnt<<'\n'
<<"the number of i"<<icnt<<'\n'
<<"the number of o"<<ocnt<<'\n'
<<"the number of u"<<ucnt<<'\n'
<<"the number of 元音总个数"<<yycnt<<endl;

}

首先,你的程序中有一个不好的地方:
在首次执行while(ch!='n')这个语句之前并没有给ch赋值。
建议声明变量ch的同时赋值。可以是除'\n'外任意的初值。

第二,你的本意应该是遇到回车后结束。你的while语句却写成了
while(ch!='n')
其实应该是
while(ch!='\n')
真正的换行符应该是\n,而不是n

知道这些,你的程序就能改正确了。

可以使用getchar、getche、getch等函数实现上面“lee富荣”的意思...

当然也可以简单地写程序:
#include<iostream>
#include<string>
using namespace std;

int main()
{
string str;
cin>>str;
int coun[26];
memset(count,0,sizeof(count));
for(int i=str.length()-1;i>=0;i--) count[str[i]-'a']++;

cout<<"the number of a:"<<count[0]<<'\n'
<<"the number of e:"<<count[4]<<'\n'
<<"the number of i:"<<count[8]<<'\n'
<<"the number of o:"<<count[14]<<'\n'
<<"the number of u:"<<count[20]<<'\n'
<<"元音总个数:"<<count[0]+count[4]+conut[8]+count[14]+count[20]<<endl;

return 0;
}

碰到输入为"n"就退出了