Stack around array of pointers was corrupted

Down below is the part of the main where pA is used. The error i'm getting is stack around pA was corrupted and i searched the net and didn't find an answer :( Can anyone help?
Is there something wrong with the while loop?

Header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef H_FractionType
#define H_FractionType

#include <iostream>
#include <string>
using namespace std;

class Person
{
protected:
	string name;
	int age;
public:
	virtual void Print() = 0;
};

class Teacher : public Person
{
private:
	int salary;
public:
	Teacher(string = " ", int = 0, int = 0);
	string GetName();
	int GetAge();
	int GetSalary();
	void SetTeacher(string, int, int);
	virtual void Print();
};

class Student : public Person
{
private:
	char grade;
	double fees;
public:
	Student(string = " ", int = 0, char = ' ', double = 0);
	string GetName();
	int GetAge();
	char GetGrade();
	double GetFees();
	void SetStudent(string, int, char, double);
	virtual void Print();
};

#endif 


Functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;

Teacher::Teacher(string a, int b, int c)
{
	name = a;
	age = b;
	salary = c;
}

Student::Student(string a, int b, char c, double d)
{
	name = a;
	age = b;
	grade = c;
	fees = d;
}

string Teacher::GetName()
{
	return name;
}

int Teacher::GetAge()
{
	return age;
}

int Teacher::GetSalary()
{
	return salary;
}

void Teacher::SetTeacher(string a, int b, int c)
{
	name = a;
	age = b;
	salary = c;
}

string Student::GetName()
{
	return name;
}

int Student::GetAge()
{
	return age;
}

char Student::GetGrade()
{
	return grade;
}

double Student::GetFees()
{
	return fees;
}

void Student::SetStudent(string a, int b, char c, double d)
{
	name = a;
	age = b;
	grade = c;
	fees = d;
}

void Teacher::Print()
{
	cout << "\nName: " << name;
	cout << "\nAge: " << age;
	cout << "\nSalary: " << salary << " L.L.\n";
}

void Student::Print()
{
	cout << "\nName: " << name;
	cout << "\nAge: " << age;
	cout << "\nGrade: " << grade;
	cout << "\nFees: " << fees << " L.L.\n";
}


And Part of the Main (where pA is being used):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;

template <class Type>
void SortAName(Type* , int);

template <class Type>
void Swap(Type& A, Type& B)
{
	Type temp = A;
	A = B;
	B = temp;
}

int main()
{
	Person* pA[4];
	Teacher a[4];
	Student b[4];
	
	string name;
	int age, salary;
	char grade;
	double fees;

	char c;
	int i = 0, j = 0, k = 0;
	while (i < 4)
	{
		cout << "Enter T for teacher or S for student:\n";
		cin >> c;
		if (c != 't' && c != 'T' && c != 's' && c != 'S') cout << "Wrong Choice. Please Try Again.\n";
		if (c == 't' || c == 'T')
		{
			pA[i] = &a[j];
			i++;
			j++;

			cout << "Enter Name:\n";
			cin >> name;
			cout << "Enter Age:\n";
			cin >> age;
			cout << "Enter Salary:\n";
			cin >> salary;
			a[j].SetTeacher(name, age, salary);
			cout << endl;
		}
		else if (c == 's' || c == 'S')
		{
			pA[i] = &b[k];
			i++;
			k++;

			cout << "Enter Name:\n";
			cin >> name;
			cout << "Enter Age:\n";
			cin >> age;
			cout << "Enter Grade:\n";
			cin >> grade;
			cout << "Enter Fsalarys:\n";
			cin >> fees;
			b[k].SetStudent(name, age, grade, fees);
			cout << endl;
		}
	}


And the other part of the Main (where pA is not used)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
cout << "\nThat was just a test! Now for the real thing :)\nEnter a number of Teachers then a number of Students.\n";

	Teacher* A;
	Student* B;
	int numb1, numb2;

	cout << "\nHow many teachers do you want to add?\n";
	cin >> numb1;
	A = new Teacher[numb1];
	cout << "\nEnter the teachers.\n";
	for (int i = 0; i < numb1; i++)
	{
		cout << "\nEnter Name:\n";
		cin >> name;
		cout << "Enter Age:\n";
		cin >> age;
		cout << "Enter Salary:\n";
		cin >> salary;
		A[i].SetTeacher(name, age, salary);
	}

	cout << "\nHow many students do you want to add?\n";
	cin >> numb2;
	B = new Student[numb2];
	cout << "\nEnter the students.\n";
	for (int i = 0; i < numb2; i++)
	{
		cout << "\nEnter Name:\n";
		cin >> name;
		cout << "Enter Age:\n";
		cin >> age;
		cout << "Enter Grade:\n";
		cin >> grade;
		cout << "Enter Fees:\n";
		cin >> fees;
		B[i].SetStudent(name, age, grade, fees);
	}
	SortAName(A, numb1);
	SortAName(B, numb2);
	cout << "\nHere's an ordered list of the teachers:\n";
	for (int i = 0; i < numb1; i++)
	{
		A[i].Print();
	}
	cout << "\nHere's an ordered list of the students:\n";
	for (int i = 0; i < numb2; i++)
	{
		B[i].Print();
	}
	delete []A;
	delete []B;
	system("pause");
	return 0;
}

template <class Type>
void SortAName(Type* A, int size)
{
	for (int i = 0; i < size; i++)
	{
		for (int j = i + 1; j < size; j++)
		{
			if (A[i].GetName() > A[j].GetName()) Swap(A[i], A[j]);
		}
	}
}
Last edited on
Can't tell without seeing the declarations for Person, Student and Teacher classes.
Post the entire program.
I updated the post.
Last edited on
anyone?
It looks like you are writing to array elements using an index that is out of bounds (line 47 & 64 in third block of code).
Last edited on
Oh right :O didn't notice that!! thanks a lot it worked :D
Last edited on
Topic archived. No new replies allowed.