I am trying to complete my first C++ Project and I can't seem to figure out how to do the following:
I am fin information from a file that looks like this:
Fname Lname (strings)
NumofGrades (int)
Grade1 Grade2 GradeN (doubles)
The file the instructor will be using can have any number of grades and students (The program is supposed to read from the file and print the student's name and average. I can fin the file and cout the information but how can I take multiple grades and users into consideration? Here is the code that I have compiled (with 0 errors). Can anyone put me on the right track??
#include<iostream>
#include<fstream>
using namespace std;
///////////////////////////////////
// Structures //
///////////////////////////////////
struct student{
char name[128];
int NumofGrades;
double Grade1, Grade2, Grade3, Grade4, Grade5;
};
///////////////////////////////////
// Prototypes //
///////////////////////////////////
void ReadStudent(istream &in, student &e);
void WriteStudent(ostream &out, student &e);
///////////////////////////////////
// Main //
///////////////////////////////////
int main(){
ifstream in;
in.open("program1.dat");
if(in.fail()){
cout<<"Error opening file!!!"<<endl;
return(0);
}
student e1;
student e2;
student e3;
student e4;
ReadStudent(in, e1);
ReadStudent(in, e2);
ReadStudent(in, e3);
ReadStudent(in, e4);
WriteStudent(cout, e1);
WriteStudent(cout, e2);
WriteStudent(cout, e3);
WriteStudent(cout, e4);
return(0);
}
///////////////////////////////////
// Definitions //
///////////////////////////////////
void ReadStudent( istream &in, student &e) {
char pbuf[128];
in>>e.name;
strcat(e.name, " ");
in>>pbuf;
strcat(e.name, pbuf);
in >>e.NumofGrades
>>e.Grade1 >>e.Grade2 >>e.Grade3 >>e.Grade4 >>e.Grade5;
}
void WriteStudent(ostream &out, student &e) {
out << "Name: " << e.name << endl;
out << "# of grades:" << e.NumofGrades << endl
<< "Grades: " << e.Grade1 <<" "<< e.Grade2 <<" "
<< e.Grade3 <<" " << e.Grade4 <<" " << e.Grade5 << endl ;
double average;
average= (e.Grade1+e.Grade2+e.Grade3+e.Grade4+e.Grade5)/e.NumofGrades;
out<< "Average:" << average << endl << endl;
}