My C++ Project class array doesn't work and the Program crashes without an error report

Viewed 73

I am working on a small practice Project to help with my Classes and Objects. When compiling, I don't get any errors and when I run the Program, I am able to input the first Value (As seen in the 'main()' function, but after that, the Program crashes without an error report other than "Process terminated with status -1073741819".

The Program is based around my College's structure. My intention was to input the size of the array (And Yes, I am aware that there isn't a 'cout' line in the main function prompting you to intput the size of the array.

This is my Class program:

#include <iostream>
#include <string>

using namespace std;

class Module
{
private:
    string Module_Name; //pretty self-explainaroty
    int Assignment_1_Total_Points; //total tally of Assignment 1 Points
    int Assignment_2_Total_Points; //total tally of Assignment 2 Points
    int Assignment_1_Max; //Max possible points for Assignment 1
    int Assignment_2_Max; //Max possible points for Assignment 2
    int Assignment_1_Question_Nr[]; //individual points of Assignment 2 Questions
    int Assignment_2_Question_Nr[]; //individual points of Assignemnt 2 Questions
    float Assignment_1_Percentage; //individual Mark for the first Assignment
    float Assignment_2_Perventage; //individual Mark for the second Assignment
    float Semester_Final_Percentage; //Final Percentage for the Semester Mark
public:
    string name; //input variable for Module_Name
    int n=0; //number of questions in Assignment 1,
    int m=0; //number of questions in Assignment 2
    int TA1=0; //total points for Assignment 1
    int TA2=0; //total points for Assignemnt 2
    int MA1=0; //Max points for Assignemnt 1
    int MA2=0; //Max points for Assignment 2
    int A1QP=0; //input variable to store in Member Variable Array
    int A2QP=0; //input variable to store in Member Variable Array
    int SumA1=0; //Sum of points to be stored
    int SumA2=0;
    Module() //constructor
    {
        Module_Name="";
        Assignment_1_Total_Points=0;
        Assignment_2_Total_Points=0;
        Assignment_1_Max=0;
        Assignment_2_Max=0;
        Assignment_1_Question_Nr[n]={};
        Assignment_2_Question_Nr[m]={};
    }
    void Set_Module_Name()//input the Modlue name and make Module_Name equal to it
    {
        cout<<"Enter the Name of the Module: ";
        getline(cin,name);
        Module_Name=name;
    }
    void set_Assignment_Question_Sizes() //setting 'n' and 'm' to input values
    {
        cout<<"Enter the number of Questions in Assignment 1: ";
        cin>>n;
        cout<<"Enter the number of Questions in Assignemnt 2: ";
        cin>>m;
    }
    void Get_Set_Max_Points() //setting the maximum Points possible for each assignment
    {
        cout<<"Enter the Maximum possible Points for Assignment 1: ";
        cin>>MA1;
        cout<<"Enter the Maximum possible Points for Assignment 2: ";
        cin>>MA2;
        Assignment_1_Max=MA1; //set the Member Variable so that it cannot be changed later on
        Assignment_2_Max=MA2; //set the Member Variable so that it cannot be changed later on
    }
    void Get_Set_Assignment_Question_Points() //function to get the individual Question Marks
    {
        for(int i=0;i<n;i++) //Input for Question 1
        {
            cout<<"Enter the Points given for Assignment 1 Question "<<i+1<<": ";
            cin>>A1QP;
            Assignment_1_Question_Nr[i]=A1QP; //storing the Question marks in the Member Variable Array
        }
        for(int j=0;j<m;j++) //Input for Question 2
        {
            cout<<"Enter the Points given for Assignment 2 Question "<<j+1<<": ";
            cin>>A2QP;
            Assignment_1_Question_Nr[j]=A1QP; //storing the Question marks in the Member Variable Array
        }
    }
    void Set_Assignment_Total() //getting the Sum Total of the given Marks for both Assignments
    {
        for(int i=0;i<n;i++)
        {
            SumA1+=Assignment_1_Question_Nr[i];
        }
        Assignment_1_Total_Points=SumA1; //storing the Sum of Assignment 1 in the Member Variable
        for(int j=0;j<m;j++)
        {
            SumA2+=Assignment_2_Question_Nr[j];
        }
        Assignment_2_Total_Points=SumA2; //storing the Sum of Assignment 2 in the Member Variable
    }
    void Get_Assignment_Marks() //calculating the individual percentages of each assignment
    {
        Assignment_1_Percentage=(Assignment_1_Total_Points/Assignment_1_Max)*100;
        Assignment_2_Perventage=(Assignment_2_Total_Points/Assignment_2_Max)*100;
    }
    void Show_Details() //Outputs the Module Name, Assignment 1 Mark, Assignment 2 Mark and the Total Mark
    {
        cout<<"Module Name:\t\t "<<Module_Name<<endl;
        cout<<"Assignment 1 Mark: \t"<<Assignment_1_Percentage<<endl;
        cout<<"Assignment 2 Mark: \t"<<Assignment_2_Perventage<<endl;
        cout<<"Final Mark: \t\t"<<Semester_Final_Percentage<<endl;
    }
    void Run_Time() //combining all the required member functions into a single function
    {
        Set_Module_Name();
        set_Assignment_Question_Sizes();
        Get_Set_Max_Points();
        Get_Set_Assignment_Question_Points();
        Set_Assignment_Total();
        Get_Assignment_Marks();
        cout<<"\n\n\n";
        Show_Details();
    }
};

If I call this class without an Array-like structure, as in my main function looks like this:

int main()
{
    Module SOME_NAME;
    SOME_NAME.Run_Time();
}

However, when I try using the following Code, it crashes:

int main()
{
    int x;
    cin>>x;
    for(int i=0;i<x;i++)
    {
        Module Module[x];
        Module[x].Run_Time();
    }
    return 0;
}

If anyone who reads this is able to help me, or at least explain to me where I have gone wrong, please let me know.

Many Thanks.

1 Answers

It looks like you want to create dynamic tables here, but it wouldn't work.

int Assignment_1_Question_Nr[];
int Assignment_2_Question_Nr[];

If you want to create such table you should use pointers.

int* Assignment_1_Question_Nr;
int* Assignment_2_Question_Nr;

Module() //constructor
{
   ...
   Assignment_1_Question_Nr = new int[n];
   Assignment_2_Question_Nr = new int[m];
}

void set_Assignment_Question_Sizes() //setting 'n' and 'm' to input values
{
    cout << "Enter the number of Questions in Assignment 1: ";
    cin >> n;
    delete[] Assignment_1_Question_Nr;
    Assignment_1_Question_Nr = new int[n];

    cout << "Enter the number of Questions in Assignemnt 2: ";
    cin >> m;
    delete[] Assignment_2_Question_Nr;
    Assignment_2_Question_Nr = new int[m];
}

~Module() //destructor
{ 
    delete[] Assignment_1_Question_Nr;
    delete[] Assignment_2_Question_Nr;
}

Then it works for me with this.

int main()
{
    Module module;
    module.Run_Time();
}
Related