Originally Posted by
Yarin
Is this what your looking for?
Code:
class_average = 0;
for(index = 0; index < 20; index++)
class_average += test_score[index];
class_average /= 20;
Yeah I believe so. Except I just realized there is only 10 students in the file, so I've changed that. Thanks.
I've gotten it to compile now but when I run it, it's caught in an endless loop.
Here's the code and part of the output:
Code:
// Purpose: To classify student's grade by
// Satisfactory, Unsatisfactory, and Outstanding.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int test_score[10],
index;
float grade[10],
class_average;
string firstname[10],
lastname[10];
ifstream in_file;
in_file.open("History_Exam1.txt", ios::in);
for(index = 0; index < 10; index = index + 1)
{
in_file >> firstname[index];
in_file >> lastname[index];
in_file >> test_score[index];
}
class_average = 0;
for(index = 0; index < 10; index = index ++)
{
class_average += test_score[index];
class_average /= 10;
if(test_score[index] > class_average + 10.0)
{
cout << firstname[index] << " " << lastname[index] << "'s grade: " << test_score[index];
cout << " OUTSTANDING.";
}
else
{
if(test_score[index] < class_average - 10)
{
cout << firstname[index] << " " << lastname[index] << "'s grade: " << test_score[index];
cout << " UNSATISFACTORY.";
}
else
{
cout << firstname[index] << " " << lastname[index] << "'s grade: " << test_score[index];
cout << " SATISFACTORY.";
}
}
}
return (0);
}
Code:
ichard Miller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Ric
hard Miller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richa
rd Miller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard
Miller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard M
iller's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard Mil
ler's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard Mille
r's grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard Miller'
s grade: 55 OUTSTANDING.Richard Miller's grade: 55 OUTSTANDING.Richard Mi
The two problems I see is that it's obviously looping forever, and that it is giving an "outstanding" grade to richard miller, which it shouldn't.
Outstanding should be more than 10 points above class average.
Satisfactory should be within 10 points above or below the class average.
Unsatisfactory should be more than 10 points below class average.
What am I doing wrong??!!!