主题:C++新手,求助~~~
麻烦各位大虾帮忙看一下,这个程序的目的是找出5个学生中成绩的最高者,并且输出成绩最高者的学号和成绩,为什么下面的结果总是第一个学生的学号和成绩啊~~拜托了,泪奔中~~
#include<iostream>
using namespace std;
class Student{
public:
void display();
Student(int m,int n):num(m),score(n){}
int num;
int score;
};
void Student::display()
{cout<<num<<" "<<score<<" "<<endl;}
int main(){
Student stud[5]={
Student(1001,89),
Student(1002,88),
Student(1003,99),
Student(1004,98),
Student(1005,89)
};
Student *p=stud;
int result;
int max(Student *p,int n);
result=max(p,5);
stud[result].display();
system("pause");
return 0;
}
int max(Student *p,int n){
int k;
int max_score=0;
int flag;
for(k=0;k<n;k++){
if(max_score<(*p).score){
max_score=(*p).score;
p++;
flag=k;
}
}
return flag;
}
#include<iostream>
using namespace std;
class Student{
public:
void display();
Student(int m,int n):num(m),score(n){}
int num;
int score;
};
void Student::display()
{cout<<num<<" "<<score<<" "<<endl;}
int main(){
Student stud[5]={
Student(1001,89),
Student(1002,88),
Student(1003,99),
Student(1004,98),
Student(1005,89)
};
Student *p=stud;
int result;
int max(Student *p,int n);
result=max(p,5);
stud[result].display();
system("pause");
return 0;
}
int max(Student *p,int n){
int k;
int max_score=0;
int flag;
for(k=0;k<n;k++){
if(max_score<(*p).score){
max_score=(*p).score;
p++;
flag=k;
}
}
return flag;
}