c++的一道题错在哪?谢了
- 提问者网友:几叶到寒
- 2021-05-10 04:28
void main()
{char ch;
int ai(0),ei(0),ii(0),oi(0),ui(0),sum(0);
cout<<"请输入一串字母:";cin>>ch;
while(ch!='#')
{ switch(ch)
{case'a':ai++;break;
case'e':ei++;break;
case'i':ii++;break;
case'o':oi++;break;
case'u':ui++;break;
}
}
sum=ai+ei+ii+oi+ui;
cout<<"ai="<<ai<<endl;
cout<<"ei="<<ei<<endl;
cout<<"ii="<<ii<<endl;
cout<<"oi="<<oi<<endl;
cout<<"ui="<<ui<<endl;
cout<<"sum="<<sum<<endl;
}
- 五星知识达人网友:蓝房子
- 2021-05-10 04:45
cin >> ch;没有放在循环内,这样即使输入一串字符,程序也只会读取第一个字符。
#include <iostream>
using namespace std;
int main()
{
char ch;
int ai(0), ei(0), ii(0), oi(0), ui(0), sum(0);
cout << "请输入一串字母:";
while(cin >> ch , ch != '#')
{
switch(ch)
{
case 'a':
ai++;
break;
case 'e':
ei++;
break;
case 'i':
ii++;
break;
case 'o':
oi++;
break;
case 'u':
ui++;
break;
}
}
sum = ai + ei + ii + oi + ui;
cout << "ai=" << ai << endl;
cout << "ei=" << ei << endl;
cout << "ii=" << ii << endl;
cout << "oi=" << oi << endl;
cout << "ui=" << ui << endl;
cout << "sum=" << sum << endl;
return 0;
}
运行截图:
- 1楼网友:第四晚心情
- 2021-05-10 07:42
while(ch!='#') { switch(ch) {case'a':ai++;break; case'e':ei++;break; case'i':ii++;break; case'o':oi++;break; case'u':ui++;break; }
cin>>ch; }
不然会一直死在里面的,处理一个之后再重新读一个,直到为'#,'呵呵~小心
- 2楼网友:老鼠爱大米
- 2021-05-10 07:08
- 3楼网友:猎心人
- 2021-05-10 06:03