Hello,
i am having this following error and here bellow is my code!
The error i am getting is when b.ListA enters the function insert in the AList class.
Can anyone help?
I have no idea how and what is causing this error :(
int main()
{
AList<Major> ListM;
cout << "Hello dear user,\nDuring this program you will be able to do the following at any time:\n";
cout << "1- Add a new student\n2- Print students in any given major\n3- Print students in all majors\n4- Remove any student from any major\n";
cout << "\nFirst how about adding your first student?\n";
Student a;
Major b;
a.SetStudent();
b.SetMajor(a.GetMajor());
b.ListA.insert(a);
if (!ListM.insert(b)) cout << "Impossible to add major.\n";
int j = 0;
string s;
while (j != -1)
{
int x = 0, y = 0, count = 0;
cout << "\nWhenever you want to exit please enter - 1.\nWhich operation do you wish to use?\nPlease input the number of the chosen operation:\n";
cout << "1- Add a new student\n2- Print students in any given major\n3- Print students in all majors\n4- Remove any student from any major\n";
cin >> j;
switch (j)
{
case 1:
a.SetStudent();
x = SearchMajor(ListM, a.GetMajor());
if (x == -1)
{
b.SetMajor(a.GetMajor());
x = ListM.leftLength() + ListM.rightLength();
ListM.setPos(x);
if (b.ListA.insert(a)) SortStudent(b.ListA);
else
{
if (!ListM.insert(b)) cout << "Impossible to add major.\n";
}
}
else
{
ListM.remove(b);
b.ListA.setEnd();
if (b.ListA.insert(a)) SortStudent(b.ListA);
ListM.insert(b);
}
break;
case 2:
cout << "\nEnter the major of the students you want to print:\n";
cin >> s;
x = SearchMajor(ListM, s);
if (x == -1) cout << "Major does not exist.\n";
else
{
if (ListM.getValue(b))
{
cout << "\nThe people in " << b.GetCode() << " are:\n";
for (b.ListA.setStart(); count != b.ListA.leftLength() + b.ListA.rightLength(); b.ListA.next(), count++)
b.ListA.print();
}
}
break;
case 3:
for (ListM.setStart(); count != ListM.leftLength() + ListM.rightLength(); ListM.next(), count++)
{
if (ListM.getValue(b))
{
x = 0;
cout << "\nThe people in " << b.GetCode() << " are:\n";
for (b.ListA.setStart(); x != b.ListA.leftLength() + b.ListA.rightLength(); b.ListA.next(), x++)
b.ListA.print();
}
}
break;
case 4:
cout << "\nEnter the id of the student you want to remove:\n";
cin >> x;
cout << "Enter the major of the student you want to remove:\n";
cin >> s;
count = SearchMajor(ListM, s);
if (count == -1) cout << "Major does not exist.\n";
else
{
if (ListM.getValue(b))
{
x = SearchStudent(b.ListA, x);
if (x == -1) cout << "Student does not exist.\n";
else
{
b.ListA.setPos(x);
if (b.ListA.remove(a)) cout << "Removal of student " << a.GetId() << " majoring in " << a.GetMajor() << " successful.\n";
}
}
}
break;
case -1:
break;
default: cout << "\nWrong input.\n";
}
}
system("pause");
return 0;
}
Unhandled exception at 0x75FDC42D in EECE 330 Assignments.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x002FE608
is the error i get when debugging
all my functions (getvalue, insert...) work on the right of the fence.
fence is like a position i set and the for loop that you are mentioning is to shift all the elements to the right to make room for the extra elemennt
the AList class worked fine for ListM (the list of majors) and i did a previous version of this where i used a list of students (AList<Student>) and it worked too.
It stopped working when i introduced this object : AList<Student> ListA in the class Major.
I tried debugging and following the program but it seems so weird! i mean the values just disappear or something or for some reason i am writing on unreserved space
But the thing is i didn't touch any of the functions and listsize and fence seem to be just about right when i get this error so i have no idea where it came from