I'm clueless as to how to fix the errors in this contact information manager program. please help. Thanks in advanced

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Date
{
  int month, day, year;
};

struct Address
{
  string street, city, state, zipcode;
};


class Person
{
private:
  string firstname, lastname, homephone, mobilephone, email;
  Address address;
  Date birthdate;
public:
  string getFirstName() { return firstname; }
  string getLastName() { return lastname; }
  void load(ifstream& infile);
  void save(ofstream& outfile);
  void enter();
  void print();
  bool isOutOfOrder(Person p, char field);
};


#define PIM_SIZE 100
class PIM
{
private:
  Person contacts[PIM_SIZE];
  int numContacts;
  void sort(char field);
public:
  void load();
  void save();
  void add(Person p);
  void search(string name);
  void print(char field);
};



void Person::enter()
{
  cin.ignore (100,'\n');
  cout << "\nEnter first name: ";
  getline(cin, firstname);
  cout << "Enter last name: ";
  getline(cin, lastname);
  cout << "Enter street: ";
  getline(cin, address.street);
  cout << "Enter city: ";
  getline(cin, address.city);
  cout << "Enter state: ";
  getline(cin, address.state);
  cout << "Enter zip code: ";
  getline(cin, address.zipcode);
  cout << "Enter home phone: ";
  getline(cin, homephone);
  cout << "Enter mobile phone: ";
  getline(cin, mobilephone);
  cout << "Enter email: ";
  getline(cin, email);
  cout << "Enter birth month: ";
  cin >> birthdate.month;
  cout << "Enter birth day: ";
  cin >> birthdate.day;
  cout << "Enter birth year: ";
  cin >> birthdate.year;
}

void Person::print()
{
  cout << firstname << " " << lastname << endl;
  cout << address.street <<endl;
  cout << address.city <<", "
       << address.state
       << " " << address.zipcode <<endl;
  cout << "Home: " << homephone << " "
       << "Mobile: " << mobilephone << endl;
  cout << email << endl;
  cout << "Birthday: " << birthdate.month << "/"
       << birthdate.day << "/"
       << birthdate.year << endl;
}

void searchContacts(PIM pim.contacts[], int count)
{
  int matches = 0;
  string name;
  cout << "\nEnter first or last name to find: ";
  cin >> name;
  for (int i = 0; i < count; i++)
    {
      if (contacts[i].firstname == name) || contacts[i].lastname == name))
        {
          matches++;
          cout << "\nContact " << (i + 1) << ":" << endl;
          print(contacts[i]);
        }
    }
  cout << "\nTotal number of matching contacts: " << matches << endl;
}

void Person::save(ofstream& outfile)
{
  outfile << firstname << endl;
  outfile << lastname << endl;

  outfile << street <<endl;
  outfile << address.city <<endl
          << address.state <<endl
          << address.zipcode <<endl;
  outfile << homephone << endl
          << mobilephone << endl;
  outfile << email << endl;
  outfile << birthdate.month << endl
          << birthdate.day << endl
          << birthdate.year << endl;

}

void PIM::save()
{
  ofstream outfile;
  outfile.open("contacts.txt");
  outfile << numContacts << endl;

  for (int i = 0; i < numContacts; i++)
    {
      contacts[i].save(contacts.txt);
    }
  outfile.close();
}


bool Peron::isOutOfOrder(Person p, char field)
{
  if (field == 'F')
    {
      return (firstname < p.firstname); 
    }
  else
    {
      return (lastname < p.lastname);
    }
}


void PIM::sort(char field)
{
  Person temp;
  int minIndex;

  for (int i=0; i<count-1; i++)
    {
      minIndex = i;
      for (int j=i+1; j<count; j++)
        {
          if (isOutOfOrder(contacts[j],field)
            {
              minIndex=j;
            }
        }

      temp = contacts[i];
      contacts[i]=contacts[minIndex];
      contacts[minIndex]=temp;
    }

}


void printContacts (Person contacts[], int count)
{
  char field;

  cout << "\nSort list by [F]irst or [L]ast name: ";
  cin >> field;
  sort(field);

  for (int i = 0; i < count; i++)
    {
      cout << "\nContact " << (i + 1) << ":" << endl;
      print(pim.contacts[i]);
    }
  cout << "\nTotal number of contacts: " << count << endl;
}

int main()
{
  int numContacts = 0;
  int arraySize = 100; //always have space for extra 100 contacts
  PIM contacts;

  ifstream infile;
  infile.open("contacts.txt");

  if (!infile.fail())
    {
      infile >> numContacts;
    }

  contacts[arraySize];

  //if "contacts.txt" is already there read in all the information
  //to the array of contacts
  if (!infile.fail())
    {
     
      for (int i = 0; i < numContacts; i++)
        {
          contacts[i].load;
        }
      infile.close();
    }

  cout << "Personal information manager\n";
  string choice;
  int contactNum;

  do
    {
      cout << "\nA = Add contact" << endl;
      cout << "S = Search for contact" << endl;
      cout << "P = Print contacts" << endl; //change to lab 8
      cout << "X = Exit" << endl;
      cout << "\nEnter choice: ";
      cin >> choice;
      if (choice == "A" || choice == "a" )  //add contact
        {
          pim.contacts[numOfContacts] = save();
          numContacts++;
        }
      else if (choice == "S" || choice == "s") //search for contact
        {
          search(contacts, numContacts);
        }
      else if (choice == "P" || choice == "p") //change to lab 8
        {
          printContacts(contacts, numContacts);
        }
      else if (choice!= "X" && choice != "x")
        {
          cout << "Invalide choice!\n";
        }
    }
  while (choice != "X" && choice != "x");

  saveContacts(contacts, numContacts);
  cout << "\nContact information saved.\n";

  return 0;
}

string Person::getFirstName()
{
  return firstname;
}

string Person::getLastName()
{
  return lastname;
}

void Person::load(ifstream& infile)
{
  infile.ignore();
  getline(infile, firstname);
  getline(infile, lastname);
  getline(infile, address.street);
  getline(infile, address.city);
  getline(infile, address.state);
  getline(infile, address.zipcode);
  getline(infile, homephone);
  getline(infile, mobilephone);
  getline(infile, email);
  infile >> birthdate.month;
  infile >> birthdate.day;
  infile >> birthdate.year;
}

Recommended Answers

All 9 Replies

I'm clueless as to how to fix the errors in this contact information manager program. please help. Thanks in advanced

You're welcome in advance.

Out of 290 lines you aren't really asking us to guess what lines have errors and what the errors are, are you? Details, please...

Sorry For the Lack of detail.
THe compiler gives the following errors -

pim.cpp:106: parse error before `.'
pim.cpp: In function `void searchContacts(...)':
pim.cpp:112: `count' undeclared (first use this function)
pim.cpp:112: (Each undeclared identifier is reported only once
pim.cpp:112: for each function it appears in.)
pim.cpp:114: `contacts' undeclared (first use this function)
pim.cpp:114: parse error before `||'
pim.cpp:118: warning: implicit declaration of function `int print(...)'
pim.cpp:120: confused by earlier errors, bailing out

>>pim.cpp:106: parse error before `.'
You can't pass in pim.contacts[] , only pim Start with that.

Also when you post the errors, be sure to specify which line number in the posted code is the line number in the message. Your numbers were off by 10 for some reason.

>>pim.cpp:106: parse error before `.'
You can't pass in pim.contacts[] , only pim Start with that.

Also when you post the errors, be sure to specify which line number in the posted code is the line number in the message. Your numbers were off by 10 for some reason.

Thanks. I Replaced pim.contacts with pim, now the compiler give these messages

pim.cpp: In function `void searchContacts(class PIM, int)':
pim.cpp:104: `contacts' undeclared (first use this function)
pim.cpp:104: (Each undeclared identifier is reported only once
pim.cpp:104: for each function it appears in.)
pim.cpp:104: parse error before `||'
pim.cpp:108: warning: implicit declaration of function `int print(...)'
pim.cpp:110: confused by earlier errors, bailing out

Replace contacts with pim.contact

You've mismatched some brackets in line 104:

if [B]( [/B](contacts[i].firstname == name) || [B]([/B]contacts[i].lastname == name))

I've added two open-brackets.

[edit]

Replace contacts with pim.contact

I think you mean: pim.contact[B]s[/B][i]

You've mismatched some brackets in line 104:

if [B]( [/B](contacts[i].firstname == name) || [B]([/B]contacts[i].lastname == name))

I've added two open-brackets.

[edit]

I think you mean: pim.contact[B]s[/B][i]

That was helpful. I fixed the above issues, and now recieve these errors-

pim.cpp: In function `void searchContacts(class PIM, int)':
pim.cpp:98: declaration of `count' shadows a parameter
pim.cpp:105: member `contacts' is a private member of class `PIM'
pim.cpp:105: member `contacts' is a private member of class `PIM'
pim.cpp:109: warning: implicit declaration of function `int print(...)'
pim.cpp:109: `contacts' undeclared (first use this function)
pim.cpp:109: (Each undeclared identifier is reported only once
pim.cpp:109: for each function it appears in.)
pim.cpp: In method `void Person::save(class ofstream &)':
pim.cpp:120: `street' undeclared (first use this function)
pim.cpp: In method `void PIM::save()':
pim.cpp:141: request for member `txt' in `PIM::contacts', which is of non-aggregate type `Person[100]'
pim.cpp: At top level:
pim.cpp:147: syntax error before `::'
pim.cpp: In method `void PIM::sort(char)':
pim.cpp:165: `count' undeclared (first use this function)
pim.cpp:170: warning: implicit declaration of function `int isOutOfOrder(...)'
pim.cpp:170: warning: cannot pass objects of type `Person' through `...'
pim.cpp:171: parse error before `{'
pim.cpp:179: confused by earlier errors, bailing out
sunfire12{eadumb01}54:

>>declaration of `count' shadows a parameter
You created a variable that has the same name as one of the parameters to that function. Change one of those names to something else.

>>member `contacts' is a private member of class `PIM'
Private members of a class can only be accessed by a member of that class. You are apparently attempting to access it outside the class.

pim.cpp:105: member `contacts' is a private member of class `PIM'

Well, the compiler is right. It is, I didn't notice it earlier. Remove this error by making contacts[] public in your class.

pim.cpp: In method `void Person::save(class ofstream &)':
pim.cpp:120: `street' undeclared (first use this function)

Change street to adress.street to get rid of this.

[edit too slow..]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.