初学C++,有一个小程序希望大家能帮我找找错,谢谢

来源:百度知道 编辑:UC知道 时间:2024/06/06 16:03:39
这是一道作业题要求是:定义一个类,对象数组,给学生成绩排名,数组每个成员是一个学生的信息,里面有构造函数,利用输入给每个学生赋值,用友元函数进行排名,因为成绩是私有成员!我编的程序如下,编译和链接都正确,但运行就不对,希望大家帮我改改错!
#include<iostream>
using namespace std;
class student
{
public:
student()
{
cout<<"学号 姓名 成绩"<<endl;

cin>>num>>name>>score;

}
void printf();
friend void sort(student stu[]);
private:
int num;
char name[10];
float score;
};
void student::printf()
{
int a;
for(a=0;a<5;a++)
{
cout<<"学号"<<num<<endl;
cout<<"姓名"<<name<<endl;
cout<<"成绩"<<score<<endl;
}
}
void sort(student s[])
{
int i,j;
student x;
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
{
if(s[i].score>s[j].score)
{
x.num=s[i].num;&x.name=&s[i]

首先我想问,你确定你的的代能编译通过?难道是电脑粘贴都会出错,反正在我的机器上是通不过。

#include<iostream>
#include<string.h>
using namespace std;
class student
{
public:
student()
{
cout<<"学号 姓名 成绩"<<endl;
cin>>num>>name>>score;
}
void printf();
friend void sort(student stu[]);
private:
int num;
char name[10];
float score;
};

void student::printf()
{
int a;
cout<<"学号"<<num;
cout<<"姓名";
cout<<name;
cout<<"成绩"<<score<<endl;
}

void sort(student s[])
{
int i,j;
student x;
for(i=0;i<4;i++)
for(j=i+1;j<4;j++)
{
if(s[i].score>s[j].score)
{
x.num=s[i].num;
strcpy(s[i].name,x.name);
x.score=s[i].score;
s[i].num=s[j].num;
strcp