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;
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;
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.
#include <iostream>
#include <string>
usingnamespace 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;
}
}
}
}
constint 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++)