编一个c++程序,输入五个学生的学号、姓名、5门课的成绩,按平均分的高低排序输出

#include <iostream>
using namespace std;
struct stu{
long no;
char name[9];
int score1[5];
int score2[5];
int score3[5];
int score4[5];
int score5[5];
float ave;
};
stu student[5];
void main()
{
cout<<"请输入各学生的信息"<<endl;
long no[5];
char name[5];
int score1[5];
int score2[5];
int score3[5];
int score4[5];
int score5[5];
float ave[5];
for(int i=0;i<5;i++)
{ int sum;
cin>>no[i]>>name[i]>>score1[i]>>score2[i]>>score3[i]>>score4[i]>>score5[i];
sum=score1[i]+score2[i]+score3[i]+score4[i]+score5[i];
ave[i]=sum/5;
}
for(int a=0;a<5;a++)
{
stu temp;
for(int b=0;b<5;b++)
{
if(student[b].ave>student[b+1].ave)
{
temp=student[b];
student[b]=student[b+1];
student[b+1]=temp;
}
}
for(int k=0;k<5;k++)
{
cout<<student[k].no<<" "<<student[k].name<<" "<<student[k].ave<<endl;
}
}
}

请问我哪里错了,为什么输入时只能输入一个学生的信息?

#include <iostream>
using namespace std;
struct stu{
long no;
char name[9];
int score1;
int score2;
int score3;
int score4;
int score5;
double ave;
};
stu student[5];
void main()
{
cout<<"请输入各学生的信息"<<endl;
/*long no;
char name[5];
int score1;
int score2;
int score3;
int score4;
int score5;
float ave[5];*/
int j=1; //用来记录第几个学生
for(int i=0;i<5;i++)
{
int sum;
printf("第%d个:\n",j++);
cout<<"学号"; cin>>student[i].no;
cout<<"名字"; cin>>student[i].name;
cout<<"第一门"; cin>>student[i].score1;
cout<<"第二门"; cin>>student[i].score2;
cout<<"第三门"; cin>>student[i].score3;
cout<<"第四门"; cin>>student[i].score4;
cout<<"第五门"; cin>>student[i].score5;

sum=student[i].score1+student[i].score2+student[i].score3+student[i].score4+student[i].score5;
student[i].ave=sum/5.0;
}

double temp;
for(int b=0;b<5;b++)
{
if(student[b].ave>student[b+1].ave)
{
temp=student[b];
student[b]=student[b+1];
student[b+1]=temp;
}
}
for(int k=0;k<5;k++)
{
cout<<student[k].no<<" "<<student[k].name<<" "<<student[k].ave<<endl;
}

}

你的很乱,我帮你分析下:
结构体,五个成绩的定义不是那样,你那样就是一个学生的一门成绩有五个了,只有名字用数组,还有,结构体定义了成员,主函数就不须定义了。有个经验之谈,有实型数十最好用double,因为float没他精确。还有循环多了一个,输出了五次。还有问题发邮件问我,[email protected]追问

/*long no;
char name[5];
int score1;
int score2;
int score3;
int score4;
int score5;
float ave[5];*/
这个是用的指针吗?不懂。

追答

不是指针,这些是注释了的,没用的。/*.......*/中间的就是表示注释,和//差不多。已经在结构体中定义这些就不须在定义了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-07
ave[i]=sum/5;
这里用5.0,避免精度损失。

你这里只输入数据到score,no等几个数组了5组数据。 后面student完全没有赋值。其中name只能输入一个字符!很乱。不知道你这怎么定义的。每个student 5组数据 ? 那5个student 25组数据?
如果只有5组数据,name只有5个, 那么student结构内除了name都不要用数组。 然后cin那部分改用student数组。
第2个回答  2012-04-07
有10个学生,每个学生的数据包括:学号、姓名、三门课的成绩。写程序,要求scanf("%s",a[i].name); printf("请分别输入十行成绩(每行五列)\\n,ZYFaph
相似回答