While-Loop Help
Oct 17, 2012 at 2:43am UTC
Below is code that reads in student last and first names, various scores and calculates their averages. The first line of the input file is the key that gives max point values for each assignment. I can't seem to get the average to calculate once it starts listing student's, and for some reason it outputs the last student's info twice and then breaks the loop. Any help would be much appreciated.
the input file looks something like:
"Key Key 10 10 10 10 20 20 20 20 100 100
Washingtonian George 10 10 -1 10 20 -1 -1 20 100 80
Doe Jane -1 -1 -1 -1 -1 -1 -1 -1 -1 -1"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
studentNum = 0;
key = true ;
maxPoints = 0;
while (inFile)
{
quizNum = 0;
quizSum = 0;
hwNum = 0;
hwSum = 0;
examNum = 0;
examSum = 0;
if ( key == true )
{
inFile >> first;
while (quizNum < 4)
{
inFile >> quiz;
quizSum += quiz;
quizNum++;
}
while (hwNum < 4)
{
inFile >> hw;
hwSum += hw;
hwNum++;
}
while (examNum < 2)
{
inFile >> exam;
examSum += exam;
examNum++;
}
maxPoints = quizSum + hwSum + examSum;
average = (maxPoints/maxPoints)*100;
outFile << left << setw(3) << " " << left << setw(12) << last << left << setw(7) << first;
outFile << left << setw(6) << quizSum << left <<setw(6) << hwSum << left << setw(6) << examSum;
outFile << left << setw(7) << maxPoints << left << setw(9) << fixed << showpoint << setprecision (2) << average << endl;
outFile << left << setw(3) << "-" << left << setw(12) << "----------" << left << setw(7) << "-----" ;
outFile << left << setw(6) << "----" << left << setw(6) << "---" << left << setw(6) << "----" ;
outFile << left << setw(7) << "-----" << left << setw(9) << "-------" << endl;
key = false ;
}
else
{
inFile >> last >> first;
studentNum++;
while (quizNum < 4)
{
inFile >> quiz;
if (quiz < 0)
{
quiz = 0;
}
quizSum += quiz;
quizNum++;
}
while (hwNum < 4)
{
inFile >> hw;
if (hw < 0)
{
hw=0;
}
hwSum += hw;
hwNum++;
}
while (examNum < 2)
{
inFile >> exam;
if (exam < 0)
{
exam = 0;
}
examSum += exam;
examNum++;
}
points = quizSum + hwSum + examSum;
average = (points/maxPoints)*100;
outFile << left << setw(3) << studentNum << left << setw(12) << last.substr(0,10) << left << setw(7) << first.substr(0,5);
outFile << left << setw(6) << quizSum << left <<setw(6) << hwSum << left << setw(6) << examSum;
outFile << left << setw(7) << points << left << setw(9) << fixed << showpoint << setprecision (2) << average << endl;
}
}
output is :
# Last First Quiz HW Exam Total Average
- ---------- ----- ---- --- ---- ----- -------
Key Key 40 80 200 320 100.00
- ---------- ----- ---- --- ---- ----- -------
1 Washington Georg 30 40 180 250 0.00
2 Doe Jane 0 0 0 0 0.00
3 Doe Jane 0 0 0 0 0.00
Last edited on Oct 17, 2012 at 3:37am UTC
Topic archived. No new replies allowed.