Hey there, I need some help with the placement of constructors and how the second constructor would work (there's two of them). All I ask is for some explanation. These are the exact instructions: The default Constructor should build a dynamic array of size 3 for the grades. Another constructor must receive an integer specifying how many grades the student will store in the array (the size of the array).
And for now, I put that the default is 5 since I'm not quite sure how to implement the constructors and their function in my program.
Here's what I have so far:
(I apologize for the lack of documentation, I tend to do that once I'm done)
# include <iostream>
# include <iomanip>
# include <string>
# include <fstream>
using namespace std;
#ifndef STUDENT_H
#define STUDENT_H
// Class
class Student{
private:
string name,
last;
int age;
char gender;
int *gptr;
public:
Student();
Student(int size);
int CreateStudent();
int StoreStudent(string, string, string, int, char, int[]);
int DisplayStudent();
char StudentGrade(float);
float StudentAvg(int []);
};
#endif
// Carlo Rodriguez
// 22/3/10
// 801-08-5871
// carlo.rodriguez@uprrp.edu
// This programa creates a miniregister that allows the user
// to create classes, insert students into those classes,
// display the students records of the classes, as well as
// show the class summary.
#include "Student.h"
# include <iostream>
# include <iomanip>
# include <string>
# include <fstream>
using namespace std;
//Prototypes
bool CheckClass(string ClassName);
int CreateClass();
int DisplayClass();
void Instructions(void);
int Menu(void);
Student::Student(){
int *gptr;
gptr = new int grade[3];
}
int main()
{
// Declaring variables
int option;
Student person;
// Este while es para que el programa siga corriendo mientras que sea escogido uno de los 5 optiones.
while (option >= 1 || option <= 5){
// Display instructions
Instructions();
// Display menu
option = Menu();
// Desplega la option escogida por el usuario
cout << "You've chosen option: " << option << endl << endl;
if (option == 1){ // Creates a class if it does not exist
CreateClass();
}
else if(option == 2){ // Adds a student record to a class (if it exists)
person.CreateStudent();
}
else if(option == 3){ //
person.DisplayStudent();
}
else if(option == 4){
DisplayClass();
}
else if(option == 5){
return 0;
}
}
} // End main
//Functions
int DisplayClass (){
//This function displays the summary of the class
string name; // Variable of the first name
string last; // Variable that contains the last name
int age; // Variable of the age
char gender; // Variable of the gender
int grade[5]; // Variable of the 5 grades
int i = 0;
int gradetotal[5]={0,0,0,0,0};
int AgeTotal;
float gradeavg[5]; // Variable used to store the average of each grade
float AgeAvg = 0; // Variable used to store the average age
int male = 0;
int female = 0;
ifstream Class;
string ClassName;
cout << "Enter a class: " << endl;
cin >> ClassName;
ClassName.append(".dat");
Class.open(ClassName.c_str(),fstream::in);
if(!Class){
cout << "Opening file " << ClassName << " for reading \n\n";
cout << "The file could not be opened! " << endl;
cout << "Possible errors: " << endl;
cout << "The class does not exist. " << endl;
exit(1); // Exits program.
}
Class >> name >> last >> age >> gender >> grade[0] >> grade[1]
>> grade[2] >> grade[3] >> grade[4];
while(!Class.eof()){
gradetotal[0] += grade[0];
gradetotal[1] += grade[1];
gradetotal[2] += grade[2];
gradetotal[3] += grade[3];
gradetotal[4] += grade[4];
AgeTotal += age;
if (gender == 'M' || gender == 'm'){
male++;
}
else if (gender == 'F' || gender == 'f'){
female++;
}
i++;
Class >> name >> last >> age >> gender >> grade[0] >> grade[1]
>> grade[2] >> grade[3] >> grade[4];
}
gradeavg[0] = gradetotal[0]/i;
gradeavg[1] = gradetotal[1]/i;
gradeavg[2] = gradetotal[2]/i;
gradeavg[3] = gradetotal[3]/i;
gradeavg[4] = gradetotal[4]/i;
AgeAvg = AgeTotal/i;
// Displays the actual class summary
cout << "\n";
cout << "Exams Summary " << endl;
cout << "Exam 1 Average:" << setprecision(2) << gradeavg[0] << endl;
cout << "Exam 2 Average:" << setprecision(2) << gradeavg[1] << endl;
cout << "Exam 3 Average:" << setprecision(2) << gradeavg[2] << endl;
cout << "Exam 4 Average:" << setprecision(2) << gradeavg[3] << endl;
cout << "Exam 5 Average:" << setprecision(2) << gradeavg[4] << endl << endl;
cout << "Gender Summary " << endl;
cout << "Females:" << female << endl;
cout << "Males:" << male << endl << endl;
cout << "Age Average:" << AgeAvg << "\n" << endl;
return 0;
}
int Student::CreateStudent (){
// This function allows the user to insert as many students he/she wants
// to the class
// Variables required for the function
int age;
string name;
string last;
char gender;
int grade[5];
int n, i;
int students;
Student person;
char fullname[25];
int Age;
char Gender;
int Grade;
fstream Class;
string ClassName;
cout << "Enter a class: " << endl;
cin >> ClassName;
if (CheckClass(ClassName)==true){
cout << "How many students would you like to enter: " << endl;
cin >> students;
for (i = 0; i < students; i++){
// Asks the user how many students he/she would like to enter
// and for each one it asks the user to enter the student record
cout << "Enter student information: " << endl;
cout << "Enter first name: " << endl;
cin >> name;
//strcpy(name, fullname);
cout << "Enter last name: " << endl;
cin >> last;
//strcpy(last, fullname);
cout << "Enter age: " << endl;
cin >> age;
//age = Age;
cout << "Enter gender: (M or F)" << endl;
cin >> gender;
//gender = Gender;
cout << "Enter 5 grades: " << endl;
cout << "Enter grade 1: " << endl;
cin >> grade[0];
//grade[0] = Grade;
cout << "Enter grade 2: " << endl;
cin >> grade[1];
//grade[1] = Grade;
cout << "Enter grade 3: " << endl;
cin >> grade[2];
//grade[2] = Grade;
cout << "Enter grade 4: " << endl;
cin >> grade[3];
//grade[3] = Grade;
cout << "Enter grade 5: " << endl;
cin >> grade[4];
//grade[4] = Grade;
person.StoreStudent(ClassName, name, last, age, gender, grade); // Calls the function to store the student
}
return 0;
}
else {
cout << "Class does not exist. " << endl;
return 1;
}
}
int Student::StoreStudent(string ClassName, string name, string last, int age, char gender,int grade[5]){
// This function stores the student information into the file
ofstream Class;
ClassName.append(".dat");
Class.open(ClassName.c_str(),fstream::app);
Class << name << endl;
Class << last << endl;
Class << age << endl;
Class << gender << endl;
Class << grade[0] << endl;
Class << grade[1] << endl;
Class << grade[2] << endl;
Class << grade[3] << endl;
Class << grade[4] << endl;
Class.close();
}
int Student::DisplayStudent(){
int age;
string name;
string last;
char gender;
int grade[5];
float average;
Student person;
ifstream Class;
string ClassName;
cout << "Enter a class: " << endl;
cin >> ClassName;
cout << "\n";
if (CheckClass(ClassName)==true){
ClassName.append(".dat"); // Adds ".dat" to the file
Class.open(ClassName.c_str(),fstream::in);
Class >> name >> last >> age >> gender >> grade[0] >> grade[1] >>
grade[2] >> grade[3] >> grade[4];
while(!Class.eof()){
average = person.StudentAvg(grade);
cout << name << " " << last << setw(10) << age << setw(5)
<< gender << setw(5) << grade[0] << setw(5) << grade[1] << setw(5)
<< grade[2] << setw(5) << grade[3] << setw(5) << grade[4] << setw(5);
cout.precision(2);
cout << average;
cout << " " << person.StudentGrade(average) << endl;
Class >> name >> last >> age >> gender >> grade[0] >> grade[1] >>
grade[2] >> grade[3] >> grade[4];
}
cout << "\n" << endl;
Class.close();
return 0;
}
else {
cout << "Class does not exist. " << endl;
return 1;
}
}
float Student::StudentAvg(int grade[]){
// Function to determine the average grade of the student
int total = 0; //variable de conteo para el promedio
float avg; //variable para guardar el promedio
for (int i = 0; i < 5; i++)
{
total += grade[i];
}
avg = total/5;
return avg;
}
P.S. I omitted several functions from there and I appreciate any help whatsoever. For any clarification just let me know.