This is a school assignment and I think it's nearly done, there are probably some parts that need straightening out, but at this point I'm just trying to get my program to compile correctly.
The program depends on an input file with sample data as follows:
1000 John 9.5 9.0 8.5 8.0 8.5 87.0 92.5 86.0
2000 Tom 10.0 6.7 10.0 10.0 10.0 100.0 100.0 100.0
3000 Alice 6.9 8.0 8.0 8.0 8.0 80.0 80.0 80.0
id, name, quiz1, quiz2, quiz3, quiz4, quiz5, midterm1, midterm2, final
Here is what I have so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Function prototypes
void read_calcData(int id[], double average[], char grade[], string name[], int num_students, ifstream& in_file);
void printData(int id[], double average[], char grade[], string name[], int num_students);
void displayStats(double average[], int num_students, char grade[]);
void recordFinder(int id[], double average[], char grade[], string name[], int num_students, ifstream& in_file);
int main ()
{
string name[30];
int num_students, id[30];
double average[30];
char grade[30];
ifstream in_file;
read_calcData(id, average, grade, name, num_students, in_file);
printData(id, average, grade, name, num_students);
displayStats(average, num_students, grade);
recordFinder(id, average, grade, name, num_students, in_file);
return 0;
}
//Function reads data file and calculates average and grade of each student.
void read_calcData(int id[], double average[], char grade[], string name[], int num_students, ifstream& in_file)
{
char input_file[30];
double quiz1, quiz2, quiz3, quiz4, quiz5, midterm1, midterm2, final, worst_quiz;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Enter an input file: ";
cin >> input_file;
in_file.open(input_file);
if (in_file.fail())
{
cout << "Sorry, there was an error opening the input file" << endl;
exit (1);
}
char students[30];
while(!in_file.getline(students,30).eofbit)
{
num_students++;
}
for (int i=0; i < num_students; i++)
{
in_file >> id[i] >> name[i] >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5 >> midterm1 >> midterm2 >> final;
if (quiz1 < quiz2 && quiz1 < quiz3 && quiz1 < quiz4 && quiz1 < quiz5)
{
worst_quiz = quiz1;
}
else if (quiz2 < quiz1 && quiz2 < quiz3 && quiz2 < quiz4 && quiz2 < quiz5)
{
worst_quiz = quiz2;
}
else if (quiz3 < quiz1 && quiz3 < quiz2 && quiz3 < quiz4 && quiz3 < quiz5)
{
worst_quiz = quiz3;
}
else if (quiz4 < quiz1 && quiz4 < quiz2 && quiz4 < quiz3 && quiz4 < quiz5)
{
worst_quiz = quiz4;
}
else
{
worst_quiz = quiz5;
}
average[i] = ((((quiz1+quiz2+quiz3+quiz4+quiz5-worst_quiz)*10)/4)*.2)+(((midterm1+midterm2)/2)*.4)+(final*.4);
if (average[i] >= 90)
{
grade[i] = 'A';
}
else if (average[i] >=80)
{
grade[i] = 'B';
}
else if (average[i] >=70)
{
grade[i] = 'C';
}
else if (average[i] >=60)
{
grade[i] = 'D';
}
else
{
grade[i] = 'F';
}
}
return;
}
//Function displays student id, name, average, and grade.
void printData(int id[], double average[], char grade[], string name[], int num_students)
{
int m;
double hold;
for (int i=0; i<=num_students-2; i++)
{
m = i;
for (int j=i+1; j <= num_students-1; j++)
{
if (average[j] > average[m])
m = j;
}
hold = average[m];
average[m] = average[i];
average[i] = hold;
}
cout << "---------------------------------------------------" << endl;
cout << " Individual Course Report" << endl;
cout << "---------------------------------------------------" << endl;
for (int i=0; i < num_students; i++)
{
cout << " " << id[i] << " " << name[i] << " - " << average[i] << " " << grade[i] << endl;
}
cout << "---------------------------------------------------" << endl;
return;
}
//Function displays statistics for students including number of students, class average, and a histogram illustrating grade distribution.
void displayStats(double average[], int num_students, char grade[])
{
double average_all=0;
int gradeA=0;
char starsA=0;
cout << " " << endl;
cout << "---------------------------------------------------" << endl;
cout << " Statistics" << endl;
cout << "---------------------------------------------------" << endl;
cout << " Number of Students: " << num_students << endl;
for (int i=0; i< num_students; i++)
{
average_all += average[i];
}
average_all = average_all/num_students;
cout << " Average of All Students: " << average_all << endl;
cout << " Grade Distribution (histogram)" << endl;
cout << " A: ";
for (int i=0; i<num_students; i++)
{
if (grade[i] == 'A')
{
cout << "*";
}
}
cout << endl;
cout << " B: ";
for (int i=0; i<num_students; i++)
{
if (grade[i]=='B')
{
cout << "*";
}
}
cout << endl;
cout << " C: ";
for (int i=0; i<num_students; i++)
{
if (grade[i]=='C')
{
cout << "*";
}
}
cout << endl;
cout << " D: ";
for (int i=0; i<num_students; i++)
{
if (grade[i]=='D')
{
cout << "*";
}
}
cout << endl;
cout << " F: ";
for (int i=0; i<num_students; i++)
{
if (grade[i]=='F')
{
cout << "*";
}
}
cout << endl;
cout << "---------------------------------------------------" << endl;
cout << " " << endl;
return;
}
//Function asks the user for a student id number and displays the student's id, name, average, and grade.
void recordFinder(int id[], double average[], char grade[], int num_students, string name[], ifstream& in_file)
{
char option;
int id2;
for (int k=0; k<num_students; k++)
{
cout << "---------------------------------------------------" << endl;
cout << " Record Finder: Enter an id of a student: ";
cin >> id2;
for (int i=0; i<num_students; i++)
{
in_file >> id[i] >> name[i];
if (id2=id[i])
{
cout << "---------------------------------------------------" << endl;
cout << " ID: " << id << endl;
cout << " Name: " << name << endl;
cout << " Average: " << average << " " << grade << endl;
cout << "---------------------------------------------------" << endl;
cout << " Do you want to continue? (Y/N) ";
cin >> option;
if (option == 'n' || option == 'N')
{
cout << "---------------------------------------------------" << endl;
cout << " Done." << endl;
return;
}
}
if(id2 != id[i])
{
cout << "---------------------------------------------------" << endl;
cout << "Fail to search. Id 1500 doesn’t exist." << endl;
cout << "---------------------------------------------------" << endl;
cout << " Do you want to continue? (Y/N) ";
cin >> option;
if (option == 'n' || option == 'N')
{
cout << "---------------------------------------------------" << endl;
cout << " Done." << endl;
return;
}
}
}
}
}