Hey there, I'm having trouble getting my program to display the students and class summary using classes. What I'm unsure of is where the problem lies, whether it is a displacement of set/get functions or the content in my StoreStudent function is incorrect, or perhaps something else I might have missed. Anyways, any help, guidance, or helpful sites maybe, would be extremely helpful. This is my code so far:
# 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 size;
int *gptr;
public:
Student();
Student(int, int);
~Student();
int CreateStudent();
void StoreStudent(string, string, string, int, char, int[], int);
void DisplayStudent();
char StudentGrade(int);
int StudentAvg(int []);
void setName(string);
string getName();
void setLast(string);
string getLast();
void setAge(int);
int getAge();
void setGender(char);
char getGender();
};
#endif
// 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();
void DisplayClass();
void Instructions(void);
int Menu(void);
Student::Student(){
gptr = new int[3];
size = 0;
}
Student::Student(int n, int m_size){
gptr = new int[n];
size = m_size;
}
Student::~Student(){
delete[] gptr;
}
void Student::setName(string N){
name = N;
}
string Student::getName(){
return name;
}
void Student::setLast(string l){
last = l;
}
string Student::getLast(){
return last;
}
void Student::setAge(int a){
age = a;
}
int Student::getAge(){
return age;
}
void Student::setGender(char g){
gender = g;
}
char Student::getGender(){
return gender;
}
int main()
{
// Declaring variables
int option = 0;
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
void Instructions(){
cout << "This program allows the user to create a class and input student records. \n" << endl;
}
int Menu(){
int option;
cout << "Menu:" << endl;
cout << "\t1) Create Class" << endl;
cout << "\t2) Insert Student Record" << endl;
cout << "\t3) Display Class Students" << endl;
cout << "\t4) Display Class Summary" << endl;
cout << "\t5) Exit Program \n" << endl;
cout << "Choose an option from the Menu: " << endl;
cin >> option;
while ((option < 1) || (option > 5)){
cout << "Error: Choose between the options 1-5: " << endl;
cin >> option;
}
return option;
}
bool CheckClass(string ClassName){
// This function verifies to see whether the class exists or not
fstream Class;
ClassName.append(".dat");
Class.open(ClassName.c_str(),fstream::in);
if (Class.good()){
Class.close();
return true ;
}
return false;
}
int CreateClass (){
// This function creates a file for the class entered
fstream Class;
string ClassName;
cout << "Enter a class: " << endl;
cin >> ClassName;
if (CheckClass(ClassName)==false)
{
ClassName.append(".dat"); // Adds ".dat" to the file
Class.open(ClassName.c_str(),fstream::out);
cout << "The class " << ClassName << " was created" << endl;
Class.close();
return 0;
}
else
{
cout << "The class already exists." << endl;
return 1;
}
}
void 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 size;
int *grade;
int i = 0, k;
int *gradetotal; //={0,0,0,0,0};
int AgeTotal = 0;
int *gradeavg; // Variable used to store the average of each grade
int 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 >> size; //>> grade[0] >> grade[1] >> grade[2] >> grade[3] >> grade[4];
grade = new int[size];
for (k = 0; k < size; k++){
Class >> grade[k];
}
while(!Class.eof()){
gradetotal = new int[size];
/*gradetotal[0] += grade[0];
gradetotal[1] += grade[1];
gradetotal[2] += grade[2];
gradetotal[3] += grade[3];
gradetotal[4] += grade[4];*/
for (int m = 0; m < size; m++){
gradetotal[m] += grade[m];
}
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 = new int[size];
/*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;*/
for (int n = 0; n < size; n++){
gradeavg[n] = gradetotal[n]/i;
}
AgeAvg = AgeTotal/i;
// Displays the actual class summary
cout << "\n";
cout << "Exams Summary " << endl;
for(int j = 0; j < size; j++){
cout << "Exam " << j << "Average:" << setprecision(2)
<< gradeavg[j] << endl;
}
cout << "Gender Summary " << endl;
cout << "Females:" << female << endl;
cout << "Males:" << male << endl << endl;
cout << "Age Average:" << AgeAvg << "\n" << endl;
}
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 w_age;
string w_name;
string w_last;
char w_gender;
int w_size;
int *grade;
int j, i;
int students;
Student person;
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 >> w_name;
cout << "Enter last name: " << endl;
cin >> w_last;
cout << "Enter age: " << endl;
cin >> w_age;
cout << "Enter gender: (M or F)" << endl;
cin >> w_gender;
cout << "How many grades would you like to enter? " << endl;
cin >> w_size;
//Student Student(w_size);
grade = new int[w_size];
cout << "Now enter " << w_size << " grades:" << endl;
for (j = 0; j < w_size; j++){
cout << "Enter grade " << j + 1 << ": " << endl;
cin >> grade[j];
}
person.setName(w_name);
person.setLast(w_last);
person.setAge(w_age);
person.setGender(w_gender);
person.getName();
person.getLast();
person.getAge();
person.getGender();
person.StoreStudent(ClassName, w_name, w_last, w_age, w_gender, grade, w_size); // Calls the function to store the student
}
return 0;
}
else {
cout << "Class does not exist. " << endl;
return 1;
}
}
void Student::StoreStudent(string ClassName, string name, string last, int age, char gender,int grade[], int size){
// This function stores the student information into the file
ofstream Class;
ClassName.append(".dat");
Class.open(ClassName.c_str(),fstream::app | fstream::out);
Class << name << endl;
Class << last << endl;
Class << age << endl;
Class << gender << endl;
Class << size << endl;
Class << grade << endl;
for(int i = 0; i < size; i++){
Class << grade[i] << endl;
}
Class.close();
}
void Student::DisplayStudent(){
int w_age;
string w_name;
string w_last;
char w_gender;
int size;
int *grade;
int w_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 >> size;
Class >> name >> last >> age >> gender >> size;
grade = new int[size];
for (int i = 0; i < size; i++){
Class >> grade[i];
}
while(!Class.eof()){
w_average = person.StudentAvg(grade);
cout << name << " " << last << setw(10) << age << setw(5)
<< gender << setw(5);
for (int j = 0; j < size; j++){
cout << grade[j] << setw(5);
}
cout.precision(2);
cout << w_average;
cout << " " << person.StudentGrade(w_average) << endl;
Class >> name >> last >> age >> gender >> size;
for (int k = 0; k < size; k++){
Class >> grade[k];
}
}
cout << "\n" << endl;
Class.close();
//return 0;
}
else {
cout << "Class does not exist. " << endl;
//return 1;
}
}
char Student::StudentGrade(int avg)
{
// This function determines the letter grade
// of a student based on the average
char lettergrade; // Variable used to store the corresponding letter grade
if (avg >= 90)
lettergrade = 'A';
else if (avg >= 80)
lettergrade = 'B';
else if (avg >= 70)
lettergrade = 'C';
else if (avg >= 60)
lettergrade = 'D';
else
lettergrade = 'F';
return lettergrade;
}
int Student::StudentAvg(int grade[]){ // Still has yet to be fixed
// Function to determine the average grade of the student
int total = 0; //variable de conteo para el promedio
int avg; //variable para guardar el promedio
for (int i = 0; i < 5; i++)
{
total += grade[i];
}
avg = total/5;
return avg;
}
Any help would be appreciated, thanks, and for any clarifications, just let me know.