#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct Studentrecord
{
int student_number;
int s_1,s_2,s_m,s_t;
char grade;
};
void get_input(Studentrecord& the_student);
double score(Studentrecord& the_student);
char grade(double score);
void get_input(Studentrecord& the_student)
{
cout<<"student_number"<<endl;
cin>>the_student.student_number;
cout<<"s_1"<<endl;
cin>>the_student.s_1;
cout<<"s_2"<<endl;
cin>>the_student.s_2;
cout<<"s_m"<<endl;
cin>>the_student.s_m;
cout<<"s_t"<<endl;
cin>>the_student.s_t;
}
double score(Studentrecord& thestudent)
{
double rate1=50,rate2=25;
double percent1,percent2;
percent1=rate1/100;
percent2=rate2/100;
return((thestudent.s_t)*percent1+(thestudent.s_m)*percent2+(thestudent.s_1+thestudent.s_2)*5*percent2);
// return score;
}
char grade(double score)
{
if(score>=90) return 'A';
else if(score>=80) return 'B';
else if(score>=70) return 'C';
else if(score>=60) return 'D';
else return 'F';
}
int main()
{
Studentrecord student;
double score1;
char grade1;
char ans;
do
{ ofstream fout;
ifstream fin;
fin.open("student.dat");
fout.open("student.dat",ios::app);
if(fout.fail())
{
cout<<"----------"<<endl;
exit(1);
}
double next,sum=0;
double average;
int count=0;
while(fin>>next)
{
sum=sum+next;
count++;
}
average=sum/count;
get_input(student);
score1 = score(student);
grade1 = grade(score1);
fout<<setw(2)<<score1;
cout<<score1<<endl;
cout<<grade1<<endl;
cout<<"the average is:"<<average<<endl;
cin>>ans;
fin.close();
fout.close();
}while(ans=='y'||ans=='Y');
return 0;
}
帮忙解决下
怎么样可以使得打开的文件内容为空值的话怎么让
average=sum/count;
不执行