I have this little program ive made, its with simple structrues and uses arrays..
the program is compliing fine but does not give me the expected output, ive looked at it for a while now and still cant seem to figure it out. its not writing out to the output file like its supposed to, everytime i try to run it, it gives me an error.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Student
{
string name;
long idNum;
int creditPts;
int creditHrs;
double gpa;
};
const int SIZE = 20;
Student record[SIZE];
void ReadRecords(Student[], ifstream&);
double CalculateGpa(int creditPts, int creditHrs);
void WriteOut(Student[], ofstream&);
int main()
{
ifstream inFile;
inFile.open ("records.txt");
ofstream outFile;
outFile.open("gpa.txt");
ReadRecords(record,inFile)
;
for(int i=0; i<3; i++)
{
record[i].gpa=CalculateGpa
(record[i]
.creditPts
, record[i].creditHrs);
}
WriteOut(record,outFile);
return 0;
}
void ReadRecords(Student record[], ifstream &inFile)
{
int files;
inFile>>files;
for (int i=0; i<files; i++)
{
getline(inFile, record[i].name);
inFile>>record[i].idNum;
inFile>>record[i].creditPt
s;
inFile>>record[i].creditHr
s;
}
}
double CalculateGpa(int creditPts, int creditHrs)
{
double gpa;
gpa=creditPts/creditHrs;
return gpa;
}
void WriteOut(Student record[], ofstream& outFile)
{
for (int i=0; i < SIZE; i++)
{
outFile<< "NAME: " << record[i].name << endl;
outFile<< "GPA : " << record[i].gpa << endl;
}
}
the input file looks like this:
3
Jason Smith
3433 433 44
Michael Adams
3442 454 33
Chris James
5332 353 23
its supposed to print out the the name and the GPA of all the students.
Start Free Trial