Bubblesort is breaking my code, wont run

Hi, The below code should run a linear search and then a bubble sort on a family of 4 however i was getting errors i hadnt seen before like this:

Unhandled exception at 0x0FE031CA (msvcr120d.dll) in task3class.exe: 0xC0000005: Access violation reading location 0xAC5C47DF.

I tried to look through the dubugg report thing but im so new to this i really dont have any idea what im looking for so i tried to take out the linear search and then the bubblesort to see if it was one of these that was causing the problem and i think its the bubblesort as the code does seem to run without it.

I cant see where there would be a problem with the bubblesort, i have looked on other forums and found one answer on this forum that was related to the same program homework but it didnt help me.

Here is my code:

#include <iostream>
#include <string>

using namespace std;

class Person {
public:
string name;
int age;
Person(string _name, int _age) : name(_name), age(_age){}
Person();

void printOut()
{
cout << "The name is: " << name << " and their age is: " << age
<< endl;
}
};

int lsearch(Person family_[], int pInfam, int key)
{
for (int i = 0; i < pInfam; i++)
{
if (family_[i].age == key)
return i;
}
return -1;
}

void bubblesort(Person family[], int pInfam)
{
for (int i = 0; i < pInfam - 1; i++)
{
int numbersLeft = pInfam - i;

for (int j = 0; j < numbersLeft; j++)
{
if (family[j].age > family[j + 1].age)
{
Person temp = family[j];
family[j] = family[j + 1];
family[j + 1] = temp;
}
}
}
}

const int pInfam = 4;

int main()
{
Person family[pInfam] = { Person("Richard", 31), Person("Emelie", 32), Person("Uma", 4), Person("Hollie", 2) };

bubblesort(family, pInfam);

for (int i = 0; i < 4; i++)
{
family[i].printOut();
}

int index;
int search_age;
cout << "Choose the age of the person you are looking for: ";
cin >> search_age;
index = lsearch(family, 4, search_age);
cout << index;

if (index >= 0)
{
family[index].printOut();
}
else
{
cout << "There is no person with that age" << endl;
}
return 0;
};


I think the problem is here:

void bubblesort(Person family[], int pInfam)
{
for (int i = 0; i < pInfam - 1; i++)
{
int numbersLeft = pInfam - i;

for (int j = 0; j < numbersLeft; j++)
{
if (family[j].age > family[j + 1].age)
{
Person temp = family[j];
family[j] = family[j + 1];
family[j + 1] = temp;
}

Any hints or tips as to what is going wrong here would be greatly appreciated.
Please edit your post and make sure your code is [code]between code tags [/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

http://www.cplusplus.com/articles/jEywvCM9/
your code with indentation
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
#include <iostream>
#include <string> 

using namespace std; 

class Person {
	public:
		string name;
		int age;
		Person(string _name, int _age) : name(_name), age(_age){}
		Person(); 

		void printOut()
		{
			cout << "The name is: " << name << " and their age is: " << age
				<< endl;
		}
}; 

int lsearch(Person family_[], int pInfam, int key)
{
	for (int i = 0; i < pInfam; i++)
	{
		if (family_[i].age == key)
			return i;
	}
	return -1;
}

void bubblesort(Person family[], int pInfam)
{
	for (int i = 0; i < pInfam - 1; i++)
	{
		int numbersLeft = pInfam - i; 

		for (int j = 0; j < numbersLeft; j++)
		{
			if (family[j].age > family[j + 1].age)
			{
				Person temp = family[j];
				family[j] = family[j + 1];
				family[j + 1] = temp;
			}
		}
	}
} 

const int pInfam = 4; 

int main()
{
	Person family[pInfam] = { Person("Richard", 31), Person("Emelie", 32), Person("Uma", 4), Person("Hollie", 2) };

	bubblesort(family, pInfam);

	for (int i = 0; i < 4; i++)
	{
		family[i].printOut();
	}

	int index;
	int search_age;
	cout << "Choose the age of the person you are looking for: ";
	cin >> search_age;
	index = lsearch(family, 4, search_age);
	cout << index; 

	if (index >= 0)
	{
		family[index].printOut();
	}
	else
	{
		cout << "There is no person with that age" << endl;
	}
	return 0;
}


Running through gdb
$ gdb a.out
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b90b64 in std::string::assign(std::string const&) () from /usr/lib/libstdc++.so.6
(gdb) backtrace
#0  0x00007ffff7b90b64 in std::string::assign(std::string const&) () from /usr/lib/libstdc++.so.6
#1  0x00000000004012e1 in Person::operator= (this=0x7fffffffdcc0) at foo.cpp:6
#2  0x0000000000400db0 in bubblesort (family=0x7fffffffdc90, pInfam=4) at foo.cpp:41
#3  0x0000000000400fa5 in main () at foo.cpp:54
(gdb) frame 2
#2  0x0000000000400db0 in bubblesort (family=0x7fffffffdc90, pInfam=4) at foo.cpp:41
41                                      family[j] = family[j + 1];
(gdb) print j
$1 = 3
(gdb) print j+1
$2 = 4
(gdb)
family[4] is out of bounds and access it result in undefined behaviour
make line 36 to stop a little earlier for (int j = 0; j < numbersLeft-1; j++)
Last edited on
Hi,

Sorry for not posting the code properly it is my first time and i thought that i did put it between the [].

Thank you for looking at it, what a simple correction. Im glad that i at least found out that the problem lay in the bubblesort.

Thank you for your help
Topic archived. No new replies allowed.