Apr 18, 2011 at 11:42pm UTC
hello all! Beginning programming this year so excuse me if this is a simple error. I have been trying to figure this out but I am really frustrated at this point.
The program I am trying to get to work takes in a string student ID. 2 double test scores (max score of 200) and 5 double home work scores(max of 265) from a text file. It adds the scores up for test and homework separately then determines an average for them both separately. Then determines a final grade based on the tests counting for 60% of the total grade and the homework is the other 40%.
The problem is the scores are not calculating correctly from the first one on. And after each calculation the previous wrong score adds to the next students data. I think I need to add another loop to control ending the current students output and starting the next one but i am lost at this point. I really dont even understand why my first score is wrong unless the file input is wrong as well.
input from the file prg5Data.txt
123456789 77.2 88.3 22 28 35 45 33 35 40
234567890 97.5 90 25 30 38 48 34 35 50
345678901 82.4 77.5 22.5 27 35.5 44 35 33 48
output im getting which for some reason even the first calculation is wrong. Like my data input is incorrect or my math is? The correct score is in().
Any help would be greatly appreciated!!!!!
STUDENT ID HW AVG TEST AVG FINAL SCORE GRADE
123456789 98.1(89.8) 80.0(82.8) 87.2(85.6) B
234567890 196.2 159.9 174.4 A
345678901 294.3 239.9 261.6 A
Number of A's: 2
Number of B's: 1
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <conio.h>
using namespace std;
const int CLASS_SIZE = 30;
const int AMOUNT_OF_TESTS = 2;
const int AMOUNT_OF_HOMEWORK = 7;
const int MAX_STUDENT_ID_LENGTH = 9;
const double PERFECT_HOMEWORK_SCORE = 265;
const double PERFECT_TEST_SCORE = 200;
struct SStudent
{
string studentID;
double testGrades,
homeWorkGrades,
hwAve,
testAve,
finalScore;
char finalGrade;
};
int getInput(SStudent[], int);
void process(SStudent[], int);
void display(const SStudent[], int);
int main()
{
int totStudents;
SStudent gradeData[CLASS_SIZE];
totStudents = getInput(gradeData, CLASS_SIZE);
process(gradeData, totStudents);
display(gradeData, totStudents);
_getch();
return 0;
}
int getInput(SStudent gradeData[], int classSize)
{
int totalStudents = 0;
int totalTests = 0;
int totalHomeWork = 0;
ifstream ifile;
ifile.open("prg5data.txt");
if(ifile.fail())
{
cout<< "Can't find file prg5data.txt" << endl << endl;
_getch();
return 0;
}
for(totalStudents = 0; totalStudents < classSize; totalStudents++)
{
ifile >> gradeData[totalStudents].studentID;
if(gradeData[totalStudents].studentID.length() <= MAX_STUDENT_ID_LENGTH)
{
for(totalTests = 0; totalTests < AMOUNT_OF_TESTS; totalTests++)
ifile >> gradeData[totalTests].testGrades;
for(totalHomeWork = 0; totalHomeWork < AMOUNT_OF_HOMEWORK; totalHomeWork++)
ifile >> gradeData[totalHomeWork].homeWorkGrades;
}
else
{
cout << "StudentID: " << gradeData[totalStudents].studentID << " can't be greater than 9 integers. ";
_getch();
return 0;
}
if(!ifile)
break;
}
ifile.close();
return totalStudents;
}
void process(SStudent gradeData[], int totStudents)
{
int processIndex = 0;
int testAvgIndex = 0;
int homeWorkAvgIndex = 0;
double testScoreSum = 0;
double homeWorkSum = 0;
for (processIndex = 0; processIndex < totStudents; processIndex++)
{
for(testAvgIndex = 0; testAvgIndex < AMOUNT_OF_TESTS; testAvgIndex++)
{
testScoreSum += gradeData[testAvgIndex].testGrades;
}
for(homeWorkAvgIndex = 0; homeWorkAvgIndex < AMOUNT_OF_HOMEWORK; homeWorkAvgIndex++)
{
homeWorkSum += gradeData[homeWorkAvgIndex].homeWorkGrades;
}
gradeData[processIndex].testAve = ((testScoreSum / PERFECT_TEST_SCORE) * 100);
gradeData[processIndex].hwAve = ((homeWorkSum / PERFECT_HOMEWORK_SCORE) * 100);
gradeData[processIndex].finalScore = (gradeData[processIndex].testAve * .6) + (gradeData[processIndex].hwAve * .4);
if (gradeData[processIndex].finalScore > 90)
gradeData[processIndex].finalGrade = 'A';
else if (gradeData[processIndex].finalScore > 80 && gradeData[processIndex].finalScore < 89.9)
gradeData[processIndex].finalGrade = 'B';
else if (gradeData[processIndex].finalScore > 70 && gradeData[processIndex].finalScore < 79.9)
gradeData[processIndex].finalGrade = 'C';
else if (gradeData[processIndex].finalScore > 60 && gradeData[processIndex].finalScore < 69.9)
gradeData[processIndex].finalGrade = 'D';
else
gradeData[processIndex].finalGrade = 'E';
}
}
void display (const SStudent gradeData[], int totStudents)
{
ofstream outFile;
outFile.open("prg6out.txt");
int displayIndex = 0;
int totA = 0;
int totB = 0;
int totC = 0;
int totD = 0;
int totE = 0;
outFile << "STUDENT ID" << setw(25) << "HW AVG" << setw(15) << "TEST AVG" << setw(20) << "FINAL SCORE" << setw(15) << "GRADE" << endl;
outFile << endl;
for(displayIndex = 0; displayIndex < totStudents; displayIndex++)
{
outFile << fixed << showpoint << setprecision(1);
outFile << gradeData[displayIndex].studentID
<< setw(25) << gradeData[displayIndex].hwAve
<< setw(15) << gradeData[displayIndex].testAve
<< setw(20) << gradeData[displayIndex].finalScore
<< setw(15) << gradeData[displayIndex].finalGrade << endl;
if (gradeData[displayIndex].finalGrade == 'A')
totA++;
else if (gradeData[displayIndex].finalGrade == 'B')
totB++;
else if (gradeData[displayIndex].finalGrade == 'C')
totC++;
else if (gradeData[displayIndex].finalGrade == 'D')
totD++;
else
totE++;
}
outFile << endl;
outFile << "Number of A's: " << setw(10) << totA << endl
<< "Number of B's: " << setw(10) << totB << endl
<< "Number of C's: " << setw(10) << totC << endl
<< "Number of D's: " << setw(10) << totD << endl
<< "Number of E's: " << setw(10) << totE << endl;
outFile.close();
}