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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int size = 20;//change here to change array size
class Student//declares class type Student
{
public:
string Firstname, Middlename, Lastname;
float GPA;//each student has 1 GPA
char Lettergrade;
int Examgrade[3];
//holds Examgrade1, Examgrade2, Examgrade3;
int Labgrade[4];
//holds Labgrade1, Labgrade2, Labgrade3, Labgrade4;
//function declarations
void Initialize(Student Compstud[]);
//function initializes all the elements
void Read_Data(ifstream& In_Data, Student Compstud[]);
//function read values from a file, these values will be used for calculations
//postcondition: replaces 0 initialization with those values
float Calc_GPA(int Examgrade[], int Labgrade[], Student Compstud[]);
//function calculates the GPA of each of the students
//Postcondition: Able to use the return values of this function to assign letter grade
char LetterGrade(float GPA, Student Compstud[]);
//Precondition: Student has a GPA calculated
//function outputs a letter grade according to the student's calculated GPA
//Grading scale: 100-90: A , 90-80: B, 80-70: C, 70-0: D: Failure
float calc_ClassAverage(float GPA, Student Compstud[]);
//Precondition: Each student has a GPA calculated
//function uses all student's GPA to calculates average grade of the class
//use pointer to point to each student's GPA and sum them up
void Display_File(ofstream Out_Data, Student Compstud[]);
//Postconditions:
//Outputs each of the 20 students' full name, 3 exam grades,
//each of the 4 lab grades, and final grade to an output file
//Outputs the average GPA of whole class
void Display_Screen(Student Compstud[]);
//Postconditions:
//Outputs each of the 20 students' full name, 3 exam grades,
//each of the 4 lab grades, and final grade to the screen
//Outputs the average GPA of whole class
};
//to "access" any student's ID number: Student.IDnumber
//to "access" a student's 1st exam grade: Student.Examgrade[0]
Student Compstud[size];
//initializes an array within type Student to hold 'size' Compsci students
ifstream In_Data;
ofstream Out_Data;
float ClassAverage;
int main()
{
//function calls begin, Still getting the "identifier" is UNDEFINED
Compstud[size].Read_Data(In_Data, Compstud);
Compstud[size].Calc_GPA(Examgrade, Labgrade, Compstud);
Compstud[size].LetterGrade(GPA, Compstud);
Compstud[size].calc_ClassAverage(GPA, Compstud);
Compstud[size].Display_File(Out_Data, Compstud);
Compstud[size].Display_Screen(Compstud);
}
void Student:: Read_Data(ifstream& In_Data, Student Compstud[])
{
In_Data.open("inputlab10.txt");
if (!In_Data.is_open())
exit(1);
for (int i = 0; i < size; i++)//loop through each student in array
{
In_Data >> Compstud[i].Firstname >> Compstud[i].Middlename >> Compstud[i].Lastname;
for (int j = 0; j < 3; j++)//loop and read in exam grades
{
In_Data >> Compstud[i].Examgrade[j];
}
for (int k = 0; k < 4; k++)//loop and read in lab grades
{
In_Data >> Compstud[i].Labgrade[k];
}
}
In_Data.close();
return;
}
float Student:: Calc_GPA(int Examgrade[], int Labgrade[], Student Compstud[])
{
int sum1, sum2;//holds value of sums
float StudGPA;
for (int i = 0; i < size; i++)//loop through each student in array
{
for (int j = 0; j < size; j++)
{
sum1 += Compstud[i].Examgrade[j];//loop and add up student's exam grades
}
for (int k = 0; k < size; k++)
{
sum2 += Compstud[i].Labgrade[k];//loop and add up student's lab grades
}
StudGPA = ((.60 * sum1) + (.40 * sum2) / 2);
Compstud[i].GPA = ((.60 * sum1) + (.40 * sum2) / 2);
} return StudGPA;//student (element's) calculated GPA
}
char Student:: LetterGrade(float GPA, Student Compstud[])
{
for (int i = 0; i < size; i++)//loop through each student in array
{
char letter = 0;//variable to hold the letter
if (Compstud[i].GPA > 90)
letter = 'A';
else if (Compstud[i].GPA > 80)
letter = 'B';
else if (Compstud[i].GPA > 70)
letter = 'C';
else if (Compstud[i].GPA > 60)
letter = 'D';
else
letter = 'F';
char Lettergrade = letter;//assign the Lettergrade value of letter.
return Lettergrade;
}
}
float Student:: calc_ClassAverage(float GPA, Student Compstud[])
{
float GPAsum;//hold all student's GPA
for (int i = 0; i < size; i++)//loop through each student in array
{
//sum all student's GPAs and store value in GPAsum
GPAsum += Compstud[i].GPA;
ClassAverage = (GPAsum / size);
return ClassAverage;
}
}
void Student:: Display_File(ofstream Out_Data, Student Compstud[])
{
for (int i = 0; i < size; i++)//loop through each student in array
{
Out_Data.open("outputlab10.txt");
if (!Out_Data.is_open())
exit(1);
//write each student's record to file
Out_Data <<
Compstud[i].Firstname << " " << Compstud[i].Middlename << " " << Compstud[i].Lastname << " "
<< Compstud[i].GPA << " " << Compstud[i].Lettergrade << " ";
for (int j = 0; j < 3; i++)//loop and output exam grades
{
Out_Data << Compstud[i].Examgrade[j];
}
for (int k = 0; k < 4; i++)//loop and output lab grades
{
Out_Data << Compstud[i].Labgrade[k];
}
Out_Data.close();
return;
}
}
void Student::Display_Screen(Student Compstud[])
{
const int width = 6;
for (int i = 0; i < size; i++)//loop through each student in array
{
cout << "Name: " << Compstud[i].Firstname << setw(width) << Compstud[i].Middlename << setw(width)
<< Compstud[i].Lastname << endl;
cout << "Exam Grades: " << Compstud[i].Examgrade[3] << setw(width) << endl;
cout << "Lab Grades: " << Compstud[i].Labgrade[4] << setw(width) << endl;
cout << "GPA: " << Compstud[i].GPA << endl;
cout << "Grade: " << Compstud[i].Lettergrade << endl;
cout << "The class grade average was" << ClassAverage << endl;
return;
}
for (int i = 0; i < size; i++)//loop through each student in array
{
Out_Data.open("outputlab10.txt");
if (!Out_Data.is_open())
exit(1);
//write each student's record to file
Out_Data <<
Compstud[i].Firstname << " " << Compstud[i].Middlename << " " << Compstud[i].Lastname << " "
<< Compstud[i].GPA << " " << Compstud[i].Lettergrade << " ";
for (int j = 0; j < 3; i++)//loop and output exam grades
{
Out_Data << Compstud[i].Examgrade[j];
}
for (int k = 0; k < 4; i++)//loop and output lab grades
{
Out_Data << Compstud[i].Labgrade[k];
}
Out_Data.close();
return;
}
}
|