Segmentation fault error

I am trying to run this program but it is giving me a segmentation fault error. Any ideas would be appreciated!! Thanks!

#include<iostream>
#include<vector>
#include<iomanip>
#include<string>
using namespace std;
class Person
{
private:
string name;
Person* bestFriendName;
int count;
public:
void setName (string n)
{
name = n;
}
string getName ()
{
return name;
}
void setBestFriend (Person *p)
{
bestFriendName = p;
}
Person* getBestFriend ()
{
return bestFriendName;
}
void setCount ()
{
count = count + 1;
}
void display ()
{
cout << "Name: " << name << endl;
cout << "Bestfriend: " << (*getBestFriend()).getName() << endl;
cout << "Popularity: " << count << endl;
}
};
int main()
{
string yourName = "";
string bestFriendName = "";
int number = 0;
bool done = false;


Person myPerson[20];
cout << "Enter name (-1 to stop): " << endl;
getline(cin, yourName);
while(yourName != "-1")
{
myPerson[number++].setName(yourName);
cout << "Enter name (-1 to stop): " << endl;
getline(cin, yourName);

if (yourName == "-1")
{
for(int i = 0; i < number; i++)
{
cout << "Enter my best friend of " << myPerson[i].getName() << ":" << endl;
getline(cin, bestFriendName);
}
}
}
for(int i = 0; i < number; i++)
{
myPerson[i].display();
}
system("pause");
return 0;
}
I don't know which IDE you are using, but if you put a breakpoint at the start of main and step through line by line you can find out which line causes the segmentation fault.
I'm pretty new to this stuff. I'm using Dev C++. I don't know what you mean by putting a breakpoint and what would I do when I find it?
I think I figured out how to do the breakpoints but I put one on the first line of main and it still gave me the error
Try clicking in the margin somewhere and expect a little dot to appear. Then to debug, there should be a debug menu option on one of the menus. Then you should use the 'Step Through' etc commands on the same menu (or the toolbar) to go through the code line by line. I am not familiar with Dev C++ so I can't be any more specific, sorry.

If you don't know how to debug though, it's well worth learning. If you google debugging plus your IDE name you might find some more detailed explanation.

I've recently read quite a lot of stuff about Dev C++ being old and unsupported. I don't really know the situation but I imagine some people might advise you to change to a different IDE.
Codeblocks and Visual C++ Express are good choices in my opinion.

Hope this helps :)
Last edited on
By the way, while you are using Dev C++, here's a site which briefly outlines debugging in it.
http://eilat.sci.brooklyn.cuny.edu/cis1_5/HowToDebug.htm

You can follow the program through line by line and see which bit gives the error.

PS: Sorry I'm not helping with the problem directly, but it would speed things along if we knew where the error is.
When you get a segmentation fault, use Debug/Step Into.
It will show you the line where the segmentation fault occurred and allows you to examine all values of variables at the time of the crash.
Topic archived. No new replies allowed.