My code for address book is as follows:

#include <iostream>

#include <cstring>

using namespace std;


struct 


{

	char name[101][30];

	char telephone[101][8];

	char email[101][20];

	char address[101][50];

	char postal[101][6];

}employee;


void enter()

{
int n;

	cout << "Number of entries?\n";

	cin >> n;

	for (int i=0; i<n; i++){

		gets_s (employee.name[i]);
gets(
		employee.email[i]);
gets(		employee.address[i]);
gets(		employee.postal[i]);
gets(		employee.telephone[i]);              
gets (		employee.telephone[i]);
}

}


void printall(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8],int n)

{

	for (int i=0; i<n; i++){

		puts (employee.name[i]);

		puts (employee.email[i]);

		puts (employee.address[i]);

		puts (employee.postal[i]);

		puts (employee.telephone[i]);

	}

}
		
void deleteentry(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])

{ 

	gets(name [100]);


    
	for (int i=0; i<100; i++)

	
	{ if (name[i]-'0'== name[100]-'0')
    
	{ 

		employee.name[i][0]='\0';

		employee.email[i][0]='\0';

		employee.address[i][0]='\0';

		employee.postal[i][0]='\0';

		employee.telephone[i][0]='\0';

	}

	}       
} 
void update(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
{
     gets(name[100]);
        int i;
        for (i=0; i<100; i++)
        { if (name[i]-'0'==name[100]-'0')
            deleteentry(name,email,address,postal,telephone);
			enter(i);
            }
}

void find(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
{
     
	gets(name[100]);
    
	for (int i=0; i<30; i++)

	{if (strcmp(name[i], name[100])==0)

	{cout << i << "\n";

	}

	}
};

void sort(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
{
	bool sorted;
	
	char temp;
	
	int limit=101, last;
	
	while  (!sorted)
    
	{
    
		sorted = true;			
    for 
		(int i = 0;  i < limit;  i++)
     	
	{

		if(name[i][0]-'0' > name[i+1]-'0')

		{		

			sorted = false;				

			temp = name[i][0];

			name[i][0] = name[i+1][0];

			name[i+1][0] = temp;

			last = i;		

		}

	}
        		limit = last;				
    		}
}
void menu ()
{
	int i,n;
	while (cin >> i, i>0){
	switch (i)
	{
	case 1: cin >> n; enter(); break;
	
	case 2: deleteentry(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
	
	case 3: update(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
	
	case 4: find(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
	
	case 5: printall(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
	
	case 6: sort(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
	
	default: cerr <<  "Please enter a valid option\n";
	}
	
	
	system ("pause");
	
	system ("cls");
	}

	};
int main
()
{
        menu();
    system ("pause");
}

The problems I face are:
1> In input one of the entries is overlapped with another, as in if employee.postal==570306
employee. address== employee.postal
2> In outputting (printing) what happens is that one of the data is not printed. Same with gets(). I used it twice, otherwise it was inputting only 4 things. I think 1 and 2 might have a connection.
Can someone suggest me what to do, so that I can debug other numerous problems?
Thanks.

Recommended Answers

All 3 Replies

Well, postal is a character array, as is address, so you can't use the == operator to do comparisons. strcmp should do it though.

I assume you don't realise that you can pass a structure directly into a function as a parameter.

struct thing{
  int x, y;
};

void thingy( thing &i_thing ) { // pass by reference or value as you feel fit
  i_thing.x = 2;
}

which should save all those parameters going into your function. A structure, basically, is a type which tidies up all that messy parameter passing. And as it's a defined type you can pass it into a function.

You might also consider using the std::string (from <string>). It allows == comparison which character arrays do not. Again, tidy way of doing things.

There are also better alternatives to system("pause")

Hey I'm writing an address program as well. If anyone could help me with it i'd greatly appreciate it. I'm aware of how terrible looking it is but if someone can decipher what's going on in my program and try to help me that'd be much appreciated. These are the requierments:

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.

Unfortunately this i sall i have so far...

cplusplus Syntax (Toggle Plain Text)

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 *);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

Help with Code Tags cplusplus Syntax (Toggle Plain Text)

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;} 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;
}

It is greatly apreciated if this is at least looked at. If for any reason this doesn't look like what programs posted should I can't figure out why because i copied a version that someone modified for me to model mine after when posting. Thanks again.

Thanks for the advice. I am working on it.

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.