C++问题

nidearong 2009-05-06 10:49:06
请教各位 这道题这么做 题目如下:
某学校的学生会换届,有A、B、C、D、E、F六名学生竞选学生会主席,参加投票的学生代表现有15名,他们在选票上写A-F字母(大小写都可),表示投了对应候选人一票,请编一程序来统计选票。
本人编写了一段代码 代码生成成功 可是运行是出错 各位谁懂得 代码如下:
#include<iostream>
using namespace std;
int main()
{
/*char a[]={'a','b','c','d','e','f'};*/

int q,w,r,y,u,o;
char ch;
cout<<"请输入你要选的学生"<<endl;

//for(int j=0;j<6;j++)
//{
// cin>>a[j];
//}
for(int i=0;i<15;i++)
{
cin>>ch;
if(ch=='a')
{
q++;
continue ;
}

else if(ch=='b')
{
w++;
continue ;
}
else if(ch=='c')
{
r++;
continue ;
}
else if(ch=='d')
{
y++;
continue ;
}
else if(ch=='e')
{
u++;
continue ;
}
else if(ch=='f')
{
o++;
continue ;
}
}
cout<<q<<w<<r<<y<<u<<o;
return 0;
}
...全文
136 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hoomien 2009-05-07
  • 打赏
  • 举报
回复
好多if else- -
换switch()吧
zhangxun2007 2009-05-07
  • 打赏
  • 举报
回复
编译错误的原因是没有给变量赋初值0。
由于if...else多分枝语句一旦其中一条成立不再进行判断其他条件,所以continue可以省略.
同时,可以把
cout < <"请输入你要选的学生" < <endl;
放入for循环,这样更具有提示作用。
并且,不区分大小写可以将
ch=='a'
改写为
ch=='a'||ch=='A'
其余类似。

具体编码如下:
#include <iostream>
using namespace std;

int main() {
int q=0,w=0,r=0,y=0,u=0,o=0;
char ch;
for(int i=0;i <15;i++)
{ cout < <"请输入你要选的学生" < <endl;
cin>>ch;
if(ch=='a'||ch=='A')
q++;
else if(ch=='b'||ch=='B')
w++;
else if(ch=='c'||ch=='C')
r++;
else if(ch=='d'||ch=='D')
y++;
else if(ch=='e'||ch=='E')
u++;
else if(ch=='f'||ch=='F')
o++;
}
cout <<q <<w <<r <<y <<u <<o;
return 0;
}
cbajing 2009-05-07
  • 打赏
  • 举报
回复
int q,w,r,y,u,o; //没有初始化,后面做q++........就会出错
建议用switch()会好点
mengde007 2009-05-07
  • 打赏
  • 举报
回复
清空缓冲区;因为回车键留在输入队列之中了;
nidearong 2009-05-06
  • 打赏
  • 举报
回复
这位大哥fflush(stdin);是什么意思
szliang123 2009-05-06
  • 打赏
  • 举报
回复
这样或许更美观些:
#include <iostream>
#include <string>
using namespace std;

const int MAX = 15;
void main()
{
int a=0,b=0,c=0,d=0,e=0,f=0;
char ch;
cout <<"请输入你要选的学生,以A,B,C,D,E,F表示" <<endl;
for(int i=0;i<MAX;i++)
{
cin>>ch;
switch(ch)
{
case 'a': case 'A': a++; fflush(stdin);break;
case 'b': case 'B': b++; fflush(stdin);break;
case 'c': case 'C': c++; fflush(stdin);break;
case 'd': case 'D': d++; fflush(stdin);break;
case 'e': case 'E': e++; fflush(stdin);break;
case 'f': case 'F': f++; fflush(stdin);break;
default: cout << "第" << i+1 << "个投票者无效" << endl;break;
}
}
cout << "总人数是:MAX人 " << "\n其中有" << (a+b+c+d+e+f) << "张选票投中候选人\n"
<< 'A' << '\t' << a << endl
<< 'B' << '\t' << b << endl
<< 'C' << '\t' << c << endl
<< 'E' << '\t' << e << endl
<< 'F' << '\t' << f << endl;
}
  • 打赏
  • 举报
回复
嗯,不用这么麻烦,在 cin>>ch; 之前fflush(stdin);就可以了。

此外用switch比较合适。
mengde007 2009-05-06
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

int main()
{

int q=0;int w=0;int r=0;int y=0;int u=0;int o=0;
char ch;
cout <<"请输入你要选的学生" <<endl;


for(int i=0;i <15;i++)
{
cin>>ch;
if(ch=='a')
{
q++;
fflush(stdin);
continue ;
}

else if(ch=='b')
{
w++;
fflush(stdin);
continue ;
}
else if(ch=='c')
{
r++;
fflush(stdin);
continue ;
}
else if(ch=='d')
{
y++;
fflush(stdin);
continue ;
}
else if(ch=='e')
{
u++;
fflush(stdin);
continue ;
}
else if(ch=='f')
{
o++;
fflush(stdin);
continue ;
}
}
cout <<q <<w <<r <<y <<u <<o;
return 0;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧