2 dimensional dynamic array ascending order

Below is this code ive written for an assignment. The program simply takes in data for a variety of students and performs a variety of tasks on the data. For some reason the ascending order function isnt working right. Ive double, triple checked the code but cant seem to find whats wrong. It would be really helpful if somebody could point out what im doing wrong.

The ascending order function is simply taking the values from a dynamic array and setting them in ascending order in a newly created dynamic array.

The students are represented in ascending order according to their roll numbers.



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


class Student
{
private:
string **ptr;
int students;
void UtilityFunction(const Student&);
public:
Student(int=0);
~Student();
void inputValues();
void outputValues();
void design();
Student& operator = (const Student&);
Student& operator == (const Student&);
Student& operator > (const Student&);
void removeStudent(const Student&);
void addStudent(const Student&);
void ascendingOrder();
void descendingOrder();
void modifyStudent(string r);
};


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

ptr=new string * [students];

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


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

void Student::design()
{
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";
}

void Student::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][b]); } cout<<endl; } cout<<endl;
}

void Student::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 Student::removeStudent(const Student& temp)
{ int a,b=0; string c;
students=temp.students;
cout<<"Enter the roll number of the student you want removed."<<endl;
cin>>c;

for(a=0; a<students; a++)
{
for(int z=0; z<6; z++)
{
if(temp.ptr[a][z]==c) { b=a; cout<<b; } }
}
cout<<b;

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

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

void Student::addStudent(const Student& temp)
{ int a;
students=temp.students+1;

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

cout<<"At what number do you want to add the new student."<<endl;
cin>>a; a=a-1;

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][j]=temp.ptr[i-1][j]; }
} }

cout<<"New Student: "<<endl;

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


Student& Student::operator = (const Student& temp)
{ int b=0;
cout<<"Do you want to remove a student from the list or add a student. 1. Add, 2. Remove"<<endl;
cin>>b;

if(b==1) { addStudent(temp); }
else if(b==2) { removeStudent(temp); }
}

Student& Student::operator == (const Student& temp)
{
if(this == &temp || temp.ptr==0) { return *this ; }

delete[]ptr;
ptr=0;

UtilityFunction(temp);
return *this;
}

void Student::UtilityFunction(const Student& temp)
{
students=temp.students;
ptr=new string * [students];
for(int a=0; a<students; a++)
{ ptr[a]=new string[6]; }

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

}

void Student::modifyStudent(string roll)
{ int c;

for(int a=0; a<students; a++)
{
for(int b=0; b<6; b++)
{
if(ptr[a][b]==roll) { c=a; }
}}

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;
cout<<"Student "<<c+1<<": "<<endl;
for(int b=0; b<6; b++)
{
getline(cin,ptr[c][b]); } cout<<endl;
}


Student& Student::operator > (const Student& temp)
{
string a="999999999";
int b,e=0;
students=temp.students;

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

while(e<students)
{ a="999999999";
for(int c=0; c<students; c++)
{
if(temp.ptr[c][3]<a) { a=temp.ptr[c][3]; b=c; }
}

for(int f=0; f<6; f++)
{
ptr[e][f]=temp.ptr[b][f];
}

temp.ptr[b][3]="999999999";
e++;
} }


int main()
{
int a,b=1,c=1;
string roll;

cout<<"Please enter the amount of students you want to enter for this program."<<endl;
cin>>a;


Student obj(a);


obj.inputValues();
clrscr();

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

obj.design();

obj.outputValues();
cout<<endl;

cout<<"Do you want to change the list by adding or removing a student. 1. Yes. 2. No."<<endl;
cin>>c;

while(c==1)
{
Student obj1(a);

obj1=obj;
clrscr();

cout<<"Your new list looks like this."<<endl;
obj1.design();
obj1.outputValues();

obj==obj1;

cout<<"Do you want to further change the list by adding/removing student. 1. Yes, 2. No"<<endl;
cin>>c;
}

cout<<"Do you want to modify any of the students. 1. Yes, 2. No."<<endl;
cin>>c;

while(c==1)
{
cout<<"Please enter the roll number of the student you want to modify."<<endl;
cin>>roll;

obj.modifyStudent(roll);

cout<<"Do you want to modify another student. 1. Yes, 2. No"<<endl;
cin>>c;
}

clrscr();
cout<<"Your new list looks like this."<<endl;
obj.design();
obj.outputValues();

cout<<"Do you want to see list in ascending order. 1. Yes, 2. No"<<endl;
cin>>c;

if(c==1)
{
Student obj1;
obj1>obj;

clrscr();
cout<<"Your list in ascending order is."<<endl;
obj1.design();
obj1.outputValues();
}


getch();


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