My program is compiling fine but my issue is with the output. The grades are not correct and the counters are not working either. If anyone can help that would be great.
//stud.cxx
#include "stud.h"
studType::studType(int idVal, float exam1Val, float exam2Val, float exam3Val)
//*********************************************
{
id = idVal;
exam1 = exam1Val;
exam2 = exam2Val;
exam3 = exam3Val;
}
studType::studType()
//*********************************************
{
id = -111;
exam1 = -100;
exam2 = -100;
exam3 = -100;
}
void studType::readRd(ifstream& inFile)
//*********************************************
{
inFile >> id >> exam1 >> exam2 >> exam3;
}
void studType::writeRd(ofstream& outFile)
//*********************************************
{
outFile.setf(ios::fixed);
outFile.setf(ios::showpoint);
outFile.precision(2);
outFile << id << setw(10) << exam1 << setw(10) << exam2 << setw(10)
<< exam3 << setw(10) << avg;
}
void studType::findAvg(studType& rd)
{
avg = float(exam1 + exam2 + exam3) / float(3);
}
bool studType:: validRd(studType& rd)
{
const int MAXEXAM = 100.00;
const int MINEXAM = 0.00;
const int MINID = 111;
const int MAXID = 999;
if(id >= MINID && id <= MAXID && exam1 >= MINEXAM && exam1 <= MAXEXAM &&
exam2 >= MINEXAM && exam2 <= MAXEXAM && exam3 >= MINEXAM && exam3 <= MAXEXAM)
return true;
else
return false;
}
float studType::findMax(studType& rd)
{
if((exam1 || exam2 || exam3) > max)
{
if(exam1 >= exam2 && exam1 >= exam3)
max = exam1;
if(exam2 >= exam1 && exam2 >= exam3)
max = exam2;
if(exam3 >= exam1 && exam3 >= exam2)
max = exam3;
}
else
return max;
}
float studType::findMin(studType& rd)
//*********************************************
{
if(exam1 || exam2 || exam3 < min)
{
if(exam1 <= exam2 && exam1 <= exam3)
min = exam1;
if(exam2 <= exam1 && exam2 <= exam3)
min = exam2;
if(exam3 <= exam1 && exam3 <= exam2)
min = exam3;
}
else
return min;
}
void studType::printMax(ofstream& outFile, studType rd)
//**********************************************************
{
if(max > 0.00 && max < 100.00)
outFile << "Max Valid Exam:" << max << endl;
else
outFile << "NO VALID MAX!" << endl;
}
void studType::printMin(ofstream& outFile, studType rd)
//********************************************************
{
if(min < 100.00 && min > -100.00)
outFile << "Min Valid Exam:" << min << endl;
else
outFile << "NO VALID EXAMS!" << endl;
}
void studType::printInvalidDataMsg(ofstream& outFile)
//********************************************************
{
outFile.setf(ios::fixed);
outFile.setf(ios::showpoint);
outFile.precision(2);
outFile << id << setw(10) << exam1 << setw(10) << exam2 << setw(10)
<< exam3 << setw(30) << "~~ Invalid Data ~~";
}
//stud.h
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
const int MAXEXAM = 100.00;
const int MINEXAM = 0.00;
const int MINID = 111;
const int MAXID = 999;
class studType
{
public:
studType(int idVal, float exam1Val, float exam2Val, float exam3Val);
studType();
void readRd(ifstream& inFile);
void writeRd(ofstream& outFile);
void findAvg(studType& rd);
bool validRd(studType& rd);
void setRd(int idVal, float examVal);
void printInvalidDataMsg(ofstream& outFile);
float findMax(studType& rd);
float findMin(studType& rd);
void printMax(ofstream& outFile, studType rd);
void printMin(ofstream& outFile, studType rd);
private:
int id;
float exam1;
float exam2;
float exam3;
float avg;
float max;
float min;
};
//extStud.h
#include <fstream>
#include <iostream>
#include <iomanip>
const int MAXEXAM2 = 100.00;
const int MINEXAM2 = 0.00;
const int MAXID2 = 999;
const int MINID2 = 111;
using namespace std;
class extStudType
{
public:
void readRd(ifstream& inFile);
void invalidId(ofstream& outFile);
void printHeading(ofstream& outFile);
void findAvg(extStudType& extStud);
void findGrade(extStudType extStud);
void printGrade(ofstream& outFile);
void printGradeStats(ofstream& outFile);
private:
int id;
float exam1;
float exam2;
float exam3;
float avg;
int aCount;
int bCount;
int cCount;
int dCount;
int fCount;
char grade;
};
//extStud.cxx
#include "extStud.h"
void extStudType::readRd(ifstream& inFile)
{
inFile >> id >> exam1 >> exam2 >> exam3;
}
void extStudType::invalidId(ofstream& outFile)
{
if(id <= MINID2 || id >= MAXID2)
outFile << "~Invalid Id~" << endl;
}
void extStudType::printHeading(ofstream& outFile)
{
outFile << "A's" << setw(6) << "B's" << setw(6) << "C's" << setw(6)
<< "D's" << setw(6) << "F's" << setw(6) << endl;
outFile << left << "--" << setw(6) << "--" << setw(6) << "--"
<< setw(6) << "--" << setw(6) << "--" << setw(6) << endl;
}
void extStudType::findAvg(extStudType& extStud)
{
avg = float(exam1 + exam2 + exam3) / float(3);
}
void extStudType::findGrade(extStudType extStud)
{
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
if(avg <= 100.00 && avg >= 90.00)
{
grade == 'A';
++aCount;
}
else
if(avg < 90.00 && avg >= 80.00)
{
grade = 'B';
++bCount;
}
else
if(avg < 80.00 && avg >= 70.00)
{
grade = 'C';
++cCount;
}
else
if(avg < 70.00 && avg >= 60.00)
{
grade = 'D';
++dCount;
}
else
if(avg < 60.00)
{
grade = 'F';
++fCount;
}
}
void extStudType::printGrade(ofstream& outFile)
{
outFile << setw(7) << grade << endl;
}
void extStudType::printGradeStats(ofstream& outFile)
{
outFile << aCount << setw(6) << bCount << setw(6) << cCount << setw(6)
<< dCount << setw(6) << fCount << setw(6) << endl;
}
#include "stud.h"
#include "extStud.h"
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("in.data");
outFile.open("out.data");
if(inFile.fail() || outFile.fail())
{
outFile << "Input file or output file opening failed" << endl;
}
outFile << "*~< Student Exam Report >~*" << endl;
outFile << "Id" << setw(10) << "Exam1" << setw(10) << "Exam2"
<< setw(10) << "Exam3" << setw(10) << "AVG" << setw(10) << "GRADE" << endl;
outFile << "--" << setw(10) << "-----" << setw(10) << "-----"
<< setw(10) << "-----" << setw(10) << "---" << setw(10) << "-----" << endl;
studType rd;
extStudType extStud;
// extStud.readRd(inFile);
rd.readRd(inFile);
while(inFile)
{
if(rd.validRd(rd))
{
rd.findAvg(rd);
extStud.findAvg(extStud);
rd.findMax(rd);
rd.findMin(rd);
extStud.findGrade(extStud);
rd.writeRd(outFile);
extStud.printGrade(outFile);
}
else
{
rd.printInvalidDataMsg(outFile);
extStud.invalidId(outFile);
}
rd.readRd(inFile);
}
outFile << " " << endl;
rd.printMax(outFile, rd);
rd.printMin(outFile, rd);
outFile << " " << endl;
extStud.printHeading(outFile);
extStud.printGradeStats(outFile);
outFile << " " << endl;
outFile << "*< end >*" << endl;
return 0;
}