怎么利用结构输入学生成绩,然后输出前三名。看看我的代码那里错了,发现不了,呜呜呜
#include"stdafx.h"
#include<iostream>
using namespace std;
#include<stdio.h>
#define N 3
#include<iomanip>
struct student
{
char name[20];
int stu_number;
int score[3];
int sum;
};
int main()
{
cout.precision(4);
struct student stua[N];
for(int i=0;i<N;i++)
{
stua[i].sum=0;
cout<<"please input student'name number and english math chinese score"<<endl;
cin>>stua[i].name;
cin>>stua[i].stu_number;
for(int k=0;k<3;k++)
{
cin>>stua[i].score[k];
stua[i].sum=stua[i].sum+(stua[i].score[k]);
}
}
for(int j=0;j<N;j++)
{
for(int i=j+1;i<N-1;i++)
{
if(stua[i].sum>stua[j].sum)
{
stua[-1]=stua[i];
stua[i]=stua[j];
stua[j]=stua[-1];
}
}
}
for(int i=0;i<3;i++)
{
cout<<stua[i].name<<" "<<stua[i].sum/3<<endl;
}
}
不看其它的
你的 stua[-1]明显的下标溢出
(C++下标溢出的结果是不可预见的,这个是程序员最怕的BUG,你居然用它,不知你哪里学来的)
另外,你既然用C++,就应该了解C++本身有排序,不用自己写的