/*从键盘上输入5个学生的数据(包括学号,姓名,年龄,3门功课的分数),
然后求出每个人的平均分,把学号,姓名,和平均分输出到磁盘文件stud.rec中,
最后从stud.rec文件中读出这些数据,并在屏幕上显示出来*/
#include"iostream.h"
#include"fstream.h"
#include"string.h"
class student
{
protected:
int number;
char *name;
int age;
float math;
float eng;
float chinese;
float avg;
public:
student(int num,char *n,int age1,float math1,float eng1,float chinese1,float avg1)
{
name=new char[strlen(n)+1];
strcpy(name,n);
number=num;
age=age1;
math=math;
eng=eng1;
chinese=chinese1;
avg1=(math1+eng1+chinese1)/3;
avg=avg1;
ofstream fout("studrec.txt"); //创建输出文件
if(!fout)
{
cout<<"cannot open file studrec\n";
}
fout<<num<<" "<<n<<" "<<avg1<<" "<<endl;
fout.close();
ifstream fin("studrec.txt",ios::end); //写入数据
if(!fin)
{
cout<<"cannot open file studrec\n";
}
char str[20]="\0";
int numb;
float av;
fin>>numb;
fin>>str;
fin>>av;
fin.close();
cout<<number<<" "<<name<<" "<<avg<<endl;
}
~student()
{
delete[]name;
}
};
int main()
{
/* student *ob[5];
for(int i=0;i<5;i++)
{
ob[i].student(114,"linh",21,87,76,80,0);
}*/
student ob1(114,"linh",21,87,76,80,0);
student ob2(115,"fdg",23,84,23,78,0);
student ob3(116,"bf",22,87,75,87,0);
student ob4(117,"df",20,89,96,90,0);
student ob5(118,"lh",21,89,73,70,0);
return 0;
}
这程序有一些需要改正的:我要输入五个人的信息,但是我的文件里只能储存最后输入的也就是只显示ob5的信息,前面被覆盖了,这点需要改正,还有一点就是我想通过数组来输入
/* student *ob[5];
for(int i=0;i<5;i++)
{
ob[i].student(114,"linh",21,87,76,80,0);
}*/
但总提示 error C2228: left of '.student' must have class/struct/union type
这是什么原因啊,请赐教!谢谢