Thank you!
What do you mean by "it should work on the *this object."?
For "3) you try to excess the 20th element in your array , where the array ends in the 19th element. remember that in arrays the count starts with 0 . even if the program compiles - you well get segmentation fault and you won't understand why."
I do understand that concept about arrays, can you point to a piece of code where you see this is a problem?
"outputlab10.txt" should contain a list of 20 students' full names, exam grades, lab grades, GPA and lettergrade. The data from the input file is used to calculate the GPA and assign the lettergrade. Those 2 items are not in the input file but are included in the output file.
I wanted to make my program so that the items of which there are multiples of (exam grades and labgrades) are looped through rather than writing cout << Compstud[i].Labgrade[0], Compstud[i].Labgrade[1], Compstud[i].Labgrade[2] etc....does that make sense? Is this not possible perhaps?
I see what you mean with the member functions/parameters and changed them.
Now the errors I get when I compile this new version below are:
Error 5 error LNK2019: unresolved external symbol "public: __thiscall Student::Student(void)" (??0Student@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'Compstud''(void)" (??__ECompstud@@YAXXZ)
Error 6 error LNK1120: 1 unresolved externals
Code:
<iostream>
#include
<fstream>
#include
<iomanip>
#include
<string>
using
namespace std;
ifstream
In_Data;
ofstream
Out_Data;
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;
//member function declarations
Student();
//empty constructor
void Initialize();
//function initializes all the elements
void Read_Data(ifstream& In_Data);
//function read values from a file, these values will be used for calculations
//postcondition: replaces 0 initialization with those values
float Calc_GPA();
//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();
//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();
//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);
//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();
//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
float
ClassAverage;
int
main()
{
//function calls begin, Still getting the "identifier" is UNDEFINED
Compstud[size].Read_Data(In_Data);
Compstud[size].Calc_GPA();
Compstud[size].LetterGrade();
Compstud[size].calc_ClassAverage();
Compstud[size].Display_File(Out_Data);
Compstud[size].Display_Screen();
}
void
Student::Read_Data(ifstream& In_Data)
{
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();
}
float
Student::Calc_GPA()
{
int sum1 = 0, sum2 = 0;//holds value of sums
float StudGPA;
for (int i = 0; i < size; i++)//loop through each student in array
{
for (int j = 0; j < 3; j++)
{
sum1 += Compstud[i].Examgrade[j];
//loop and add up student's exam grades
}
for (int k = 0; k < 4; 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()
{
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';
elseif (Compstud[i].GPA > 80)
letter =
'B';
elseif (Compstud[i].GPA > 70)
letter =
'C';
elseif (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 GPAsum = 0;//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)
{
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();
}
}
void
Student::Display_Screen()
{
constint 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;
for (int j = 0; j < 3; i++)//loop and output exam grades
{
cout << Compstud[i].Examgrade[j];
}
for (int k = 0; k < 4; i++)//loop and output lab grades
{
cout << Compstud[i].Labgrade[k];
}
cout <<
"GPA: " << Compstud[i].GPA << endl;
cout <<
"Grade: " << Compstud[i].Lettergrade << endl;
}
cout <<
"The class grade average was" << ClassAverage << endl;
}