I have to program an Address book program in C++. So far I've done about 40% of the assignment.
The main requirements of the Address Book are the following (Which I am confused about are):
1. Display 20 Contacts at a time, using the space bar to go to the next screen.
2. Search for Contacts by Name.
3. Edit Contact.

So far I have programmed the following, let me know what I need to modify in order for me to display 20 contacts at a time. Any help will be greatly appreciated :cheesy:

#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;

struct Phonebook
{
  char fname[15];
  char lname[15];
  char fullname[45];
  char number[50];
  char email[81];
};

bool openPhonebook(fstream &, char [81]);
void Displayphonelist(fstream &);
void Addnewcontacts(fstream &);
void Deletecontacts(fstream &);
void Editinfo();
void Searchcontact(fstream &);

int main()
{
  fstream info;

  int choice = 0;
  int count = 0;

  cout << "1. Display Contact List\n";
  cout << "2. Add a new contact\n";
  cout << "3. Delete a personal contact\n";
  cout << "4. Edit personal contact information\n";
  cout << "5. Search for contact\n";
  cout << "6. Exit Program\n";

  cout << "\nPlease enter choice number: \n";
  cin >> choice;

  switch(choice)
  {
  case 1: 
    {
      if(!openPhonebook(info, "Contactlist.txt"))
      {
        cout << "File open error!\n";
        exit(1);	//Exits the program on error
      }

      cout << "File opened successfully.\n";
      cout << "Now reading data from file.\n\n";
      Displayphonelist(info);
      info.close();
      cout << "\nDone\n";
      break;
    }
  case 2: 
    {	
      cout << "Add a new contact\n";


      Addnewcontacts(info);
      cout << "\nSaving Information....\n";
      break;
    }
  case 3: cout << "Delete a personal contact\n";
    break;
  case 4: cout << "Edit personal contact information\n";
    break;
  case 5: cout << "Searching for contacts";
    Searchcontact(info);
    cout << "\nDone\n";
    break;
  case 6: cout << "Goodbye!\n";
    exit(1);

  default: cout << "Please enter valid choice\n";
    cin >> choice;
  }

  return 0;
}
// END OF MAIN

bool openPhonebook(fstream &file, char *name)
{
  bool status;

  file.open(name, ios::in);
  if (file.fail())
    status = false;
  else
    status = true;
  return status;
}

void Displayphonelist(fstream &file)
{	
  Phonebook Info;

  file.getline(Info.fullname, 45);
  file.getline(Info.number,50);
  file.getline(Info.email, 81);

  while(!file.eof())
  {
    cout << Info.fullname << endl;
    cout << Info.number << endl;
    cout << Info.email << endl;

    file.getline(Info.fullname, 45);
    file.getline(Info.number, 50);
    file.getline(Info.email, 81);
  }
}

void Addnewcontacts(fstream &file)
{	
  Phonebook Info;
  char choice;
  do
  {
    cout << "Opening file.....\n";
    file.open("Contactlist.txt", ios::out| ios::app);
    cout << "File open successfully\n";

    if(!file)
    {
      cout << "Error opening Contactlist.txt\n";
      exit(1);
    }

    if(!file.eof())
    {	
      cout << "Enter first name: ";
      cin >> Info.fname;
      cout << "Enter last name: ";
      cin >> Info.lname;
      strcpy(Info.fullname, Info.fname);
      strcat(Info.fullname, " ");
      strcat(Info.fullname, Info.lname);

      cout << "Enter number: ";
      cin.ignore();
      cin.getline(Info.number, 50);

      cout << "Enter email: ";
      cin.getline(Info.email, 81);

      file << "Name: " << Info.fullname << endl;
      file << "Number: " << Info.number << endl;
      file << "Email: " << Info.email << endl;
      file << "\n";

      file.close();	
    }

    cout << "Enter another contact?\n";
    cin >> choice;
  }while((choice == 'y')||(choice == 'Y'));

  if((choice == 'n')||(choice == 'N'))
  {
    file.close();
  }
}

void Searchcontact(fstream &file)
{	
  Phonebook Info;
  char fname2[15];
  char lname2[15];
  char fullname2[45];

  cout << "Opening file...\n";
  file.open("Contactlist.txt", ios::in);
  cout << "File open successfully\n";

  while(!file)
  {
    cout << "Error opening file\n";
    exit(1);
  }

  while(!file.eof())
  {
    cout << "Search by Full Name: \n";
    cout << "First Name: \n";
    cin >> fname2;
    cout << "Last Name: \n";
    cin >> lname2;
    strcpy(fullname2, fname2);
    strcat(fullname2, "");
    strcat(fullname2, lname2);

    if(strcmp(fullname2, Info.fullname))
    {
      cout << "There is no match\n";
      file.close();
    }
    else
    {
      cout << "Name Found!\n";
      cout << fullname2 << endl;
      cout << Info.fullname << endl;

      file.getline(Info.fullname, 45);
      file.getline(Info.number, 50);
      file.getline(Info.email, 81);

      cout << "\nDone\n";
    }
  }
}

[edit]
I added code tags for you this time. Don't post again until you understand that code is easier to read with them. I also took the time to remove excessive newlines from your butt ugly formatting, changed tabs to spaces because tabs can be erratic, and made everything neat and tidy without actually changing the meat of your code. You're welcome. Now do me a favor and do the same thing without my help in the future. -Narue
[/edit]

Hey I was looking online for a solution to a program I'm trying to write and came accross this message. I'm taking a programming class and it's getting pretty tough. I barely understand what's going on anymore. Anyway, i've been writing a program that has to be seperated into different files and i'll show you guys what i have so far. Btw, these are the requirements.

************Read “AddressBook.txt” file (if the file exists) Module:

Insert each entry into the array of structures by name
a. First record (line) contains the number of entries
b. Entry consists of the following information
Name (maximum 30 characters)
BirthDate (mm/dd/yyyy)
Address Information
Type (maximum 8 characters: Friend, Family, Business)
c. Address Information consists of the following information:
Street (maximum 35 characters)
City (maximum 20 characters)
State (2 characters)
Zip (5 characters)
Phone (14 characters)

4) User can choose the following options:
a. List all entries. This option will result in an alphabetic list.
b. Look up a specific entry by Name (use binary search). Ask user for the desired name. Output error message if the name is not found otherwise output the information.
c. List all the entries for a specific Type. Ask user for the desired type (Friend, Family, Business). Output the type followed by all the entries that match this type (i.e. Friend). This will also result in an alphabetic listing.***********************


With that being said I don't have much coding done because of how there's so many different approaches to this program. Just to warn whoever looks at this i'm aware that a lot of this is incomplete and a little sloppy.


This is the header file

int NAME_LENGTH;
int BIRTHDAY_LENGTH;
int ADDRESS_LENGTH;
int MAX_LISTING;
char relation [relation_+1];    


struct addressType
{
       string street;
       string city;
       string state;
       string zip;
       string phone;
};


struct contactType
{
       char name[NAME_LENGTH];
       int Bday;
       addressType address;
       char relation;
};
contactType newContact;
       
       contactType contacts[100];
     
       
       
       
void inputName (ifstream &, char , nameType &);
void inputBday (ifstream &, int , BdayType &);
void inputAddress (ifstream &, int , addressType &);
void inputRelation (ifstream &, char , relationType &);
int extern search (const nameType [], char, const char *);

This is the main

int main ()
{
    
    int numNames, numBdays, numAdds, index, location;
    char searchArg[birthday_LENGTH+1];
    ifstream infile;
    ofstream outfile;
    contactType allcontact[MAX_CONTACTS]
    
  
    
    
    
     infile.open (".AddressBook.txt");
    assert (!infile.fail());
    
    outfile.open ("./contacts.txt");
    assert (!outfile.fail());
    
     inputName ();
     
     
     inputBday ();
    
    
    
    searchArg[birthday_LENGTH] = '\0';
    while (true)
    {
    cout << "\n\nEnter a Birthday(mm/dd/yyyy) or 0000000 to end: ";
        cin >> searchArg;
        if (strcmp (searchArg, "0000000") == 0)
            break;
}


    infile.close();




And this one I think is the input

for (int j = 0; j < 100; j++)
           cin >> contacts[j].name.first >> contacts[j].name.middle
               >> contacts[j].name.last;
               
       for (int j = 0; j < 100; j++)
           cin >> contacts[j].Bday.month >> contacts[j].Bday.day
               >> contacts[j].Bday.year;
       
       for (int j = 0; j < 100; j++)
           cin >> contacts[j].address.street >> contacts[j].name.state
               >> contacts[j].address.zip >> contacts[j].address.phone;
       
         for (int j = 0; j < 100; j++)
           cin >> contacts[j].relation.Friend >> contacts[j].relation.Family
               >> contacts[j].relation.business;


I know that this needs a lot of changes.
I also have an example of binary coding so if sum1 could show me where my information would go that would be appreciated as well.


int search (const studentType list[], int length, const char * searchArg)
{
    bool found = false;
    int low = 0, high = length-1, mid;

    while (!found && low <= high)
    {
        mid = (low + high) / 2;
        if (strcmp(searchArg, list[mid].ID) == 0)
            found = true;
	else
	    if (strcmp(searchArg, list[mid].ID) < 0)
		    high = mid - 1;
	    else
		    low = mid + 1;
    }

    if (found)
	   return mid;
    else
	   return -1;
}

Thanks for whoever looks at and helps me with my poor programming.

Thanks a lot i really appreciate it. I don't know why but coding became 100x's harder this time around.

This article has been dead for over six months. Start a new discussion instead.