2 dimensional dynamic array

I was tasked to make a student class for my project. i was supposed to take input from the user and show the resultant output using dynamic arrays. Then i need to ask the user if he/she wants to add or delete a student entry and based on his/her answer i need to dynamically allocate more memory using a copy constructor. Everything was working fine up untill the input and output parts but things have gotten confusing as i started tackling the copy constructor. There are no errors in the program but it doesnt work properly. Especially the assignment operator function and everything that follows it. Any help or tips would be helpful.

PS. I know the logic of the whole program is very confusing. sorry about that.




#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <cstring>


class Matrix
{
private:
string **ptr;
int students;
void UtilityFunction(const Matrix&, int x, int y);
public:
Matrix(int=0);
~Matrix();
void inputValues();
void outputValues();
Matrix& operator = (const Matrix&);
void operator + (Matrix &);
void operator * (Matrix &);
};


Matrix::Matrix(int s)
{
students=s;

ptr=new string * [students];

for(int a=0; a<students; a++)
{ ptr[a]=new string[6]; }
}


Matrix::~Matrix()
{
for(int a=0; a<students; a++)
{ delete[]ptr[a]; }
delete[]ptr;
}

void Matrix::inputValues()
{
cout<<"Please enter the students full name, semester number, module number, roll number, subject 1, and subject 2. Please keep your entries to within ten characters."<<endl;
for(int a=0; a<students; a++)
{ cout<<"Student "<<a+1<<": "<<endl;
for(int b=0; b<6; b++)
{
getline(cin,ptr[a]); } cout<<endl; } cout<<endl;

}


void Matrix::outputValues()
{
cout<<endl;
int c=1,d=4;
for(int a=0; a<students; a++)
{
for(int b=0; b<6; b++)
{ gotoxy(c, d);
cout<<ptr[a][b]<<" "; c=c+14; } c=1; d=d+1; cout<<endl;} cout<<endl;
}


void Matrix::UtilityFunction(const Matrix& temp,int a,int b)
{
students=temp.students;

if(b==1){

ptr=new string * [students+1];
for(int i=0; i<6; i++)
{ ptr[i]=new string[6]; }

for(int i=0; i<students; i++)
{
for(int j=0; j<6; j++)
{ ptr[i][j]=temp.ptr[i][j]; } }

for(int i=0; i<6; i++)
{
getline(cin,ptr[students+1][i]); }

return;}


ptr=new string * [students];
for(int i=0; i<6; i++)
{ ptr[i]=new string[6]; }

for(int i=0; i<students; i++)
{
for(int j=0; j<6; j++)
{
if(i<a) { ptr[i][j]=temp.ptr[i][j]; }
else if(i>a) { ptr[i-1][j]=temp.ptr[i][j]; }
}}
}

Matrix& Matrix::operator = (const Matrix& temp) [b]//This function doesnt run

{ int a=0,b=0;
string c;

cout<<"Do you want to remove a student from the list or add a student. 1. Add, 2. Remove"<<endl;
cin>>b;

if(b==2){ cout<<"Enter the roll number of the student you want removed."<<endl;
cin>>c;

for(int a=0; a<students; a++)
{
for(int z=0; z<6; z++)
{
if(ptr[a][z]==c) { UtilityFunction(temp,a,b); return *this; } } }
}

else{ UtilityFunction(temp,a,b);
return *this; }
}


int main()
{
int a,b; string h;
cout<<"Please enter the amount of students you want to enter for this program."<<endl;
cin>>a;


Matrix obj(a);


obj.inputValues();
clrscr();

cout<<"The list of students you entered is: "<<endl;

gotoxy(1, 3); cout<<"Stud. Name";
gotoxy(15, 3); cout<<"Semester";
gotoxy(29, 3); cout<<"Module";
gotoxy(43, 3); cout<<"Roll No";
gotoxy(57, 3); cout<<"Subject 1";
gotoxy(71, 3); cout<<"Subject 2";



obj.outputValues();

cout<<endl;

Matrix obj1=obj;


getch();


}
Last edited on
Topic archived. No new replies allowed.