0

I'm trying to design an address book, trying to do a search, but I keep getting a segmentation error. I know what this means but why is it trying to allocate memory that is in use? Why is it not searching?

Here is the code:

class contact{

private:

    string fn="", ln="", email="", number="";

public:
    void input();
    void output();
    void setfname(string f_n);
    void setlname(string l_n);

    void setemail(string emaila);

    void setnumber(string num);
    string getfname();
    string getlname();

    string getemail();

    string getnumber();

contact();
contact(string f_n, string l_n, string emaila,string num);

};
void menu(string opt);

int search_contacts( contact contacts[],int MAX, string search);



int main(){

    string search;

    const int MAX=2;

    contact contacts[MAX];

    cout << "Enter up to " << MAX << " contacts.\n";

    for (int i = 1; i <= MAX; i++)

    {

     contacts[0].input();

    }

    [menu function goes here]

     contacts[0].output();

return 0;

}
 int search_contacts(contact contacts[],int size, string search)

{
 cout << "Search contact by LastName:____  ";

 cin >> search;

  bool lookup = false;

  for(int index=0;index<size;index++)

    {

      if(contacts[index].getlname() == search)

     { 

      lookup = true;

      cout<<"Result found "<<endl;

      break;

    }

     }

if(!lookup)

cout<<"no record found"<<endl;

}

void menu(string opt){
 string search;
contact contacts[0];
const int MAX = 2;

[show menu msg...here]

     cin>>opt;

// do this if the option selected is search 

if(opt=="search")

  {

    search_contacts(contacts, MAX, search);

}
[more menu code]
}
void contact::input()
{

cout<<"fn and ln separate by a space: ";

cin>>fn>>ln;

cout<<"email: ";

cin>>email;

cout<<"phone number: ";

cin>>number;

}

void contact::output()
{
[output code... goes here];

}
string contact::getlname()
{

return ln;

}

[functions that delegates the variables...goes here]
Bart
  • 19,692
  • 7
  • 68
  • 77
user618879
  • 319
  • 1
  • 3
  • 12

2 Answers2

1

As stated in the comments, you should learn how to use a debugger to determine why you get the segmentation fault.

It looks to me that you are trying to do operations on contacts that don't exist. I see a zero-length array in menu() and I don't see how you got the contacts you created in main() to the menu() function and hence to your search function.

If you try to do operations on uninitialized objects, you'll see a segmentation fault like this.

mpontillo
  • 13,559
  • 7
  • 62
  • 90
  • Indeed, this is most probably the problem. You give a zero-sized array to the search function, but set the size argument to two. This cannot work. One more hint: use vector instead of arrays, they are much easier to handle! – ElektroKraut Oct 07 '12 at 12:12
0

You cant do a string comparison using == which you have done when you match opt to "search" and try to find matching names. http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

UKM
  • 305
  • 1
  • 3
  • 11
  • Yes, this was another obvious problem that I saw. The pointer to the search string is what will be compared using `==`. It will never be equal to the pointer in the contact list. – mpontillo Oct 28 '11 at 20:15
  • 2
    I don't see the `strcmp` function in the post. The "current post" uses `std::string`, which allows the `operator==()`. – Thomas Matthews Oct 28 '11 at 20:19
  • @Thomas, thanks, I think you're right. (I'm more of a C person, so I overlooked the use of `string`.) That would make this answer incorrect. For the record, use of `==` is a glaring error in C or Java. In C++ or C# I think it's fine. (though in C# it made me cringe, coming from a Java background!) – mpontillo Oct 28 '11 at 22:17