How to use array of structs to store address information

Hey guys, I am new of structures and need some more help. I am using an array of 250 structs to hold new address entrys. However, I am confused as how to input data for each struct in the array. If you could help me with this, I would highly appreciate it!
#include <iostream>
#include <fstream>
 
using namespace std;
 
 
 
void addContact();
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
 
int main()
{
	
	string fileName;
	int menuChoice;
	fstream fileData;
	
	bool validData;
	int counter = 0;
	struct birthdate
	{
		int date;
		int month;
		int year;
	};
	struct contactParameter
	{
		char name[50];
		char cellPhoneNumber[50];
		char emailAddress[50];
		birthdate birthday;
		enum personalOrBusiness
		{
			personal,
			business
		};
		personalOrBusiness pOrBType;
		
	};		
	contactParameter contact[250];
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	cin >> fileName;
	fileData.open(fileName.c_str());
	
	for(int i = 0; i< 250; i++)
	{
		if(!contact[i].validData)
		{
			counter = i;
			break;
		}
	}
	cout << counter;
 
	cout << "Welcome to the address book application. Please select a task from the menu.\n\n"
		 << "1) Add a new contact \n\n2) Update a contact's informantion \n\n3) Delete a contact \n\n"
		 << "4) Search for a contact by name or address \n\n5) Print personal or business contacts in"
		 << " aplphabetical order by name\n\n 6) Exit the Program"; 
		
	 cin >> menuChoice;
	 switch(menuChoice)
	 {
		case 1:
			addContact(int counter);
			break;
		case 2:
			updateContact();
			break;
		case 3:
			deleteContact();
			break;
		case 4:
			searchContact();
			break;
		case 5:
			printContacts();
			break;
		case 6:
			exit(1);
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			main();
			break;
	 }
}
 
void addContact(int counter)
{
	cout << "To create a new contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just press enter.\n\n";
	cout << "Name?\n\n";
	cin.getline(contact.name,50);
	cout << "Cellphone number?\n\n";
	cin.getline(contact.cellPhoneNumber,50);
	cout << "Email address?\n\n";
	cin.getline(contact.emailAddress,50);
	cout << "Birthday day?\n\n";
	cin.getline(contact.birthday.date,50);
	cout << "Birthday month?\n\n";
	cin.getline(contact.birthday.month,50);
	cout << "Birthday year?\n\n";
	cin.getline(contact.birthday.year,50);
	cout << "Contact type?\n\n";
	cin.getline(contact.contactType,50);
	contact++;
	
	
}
 
void updateContact()
{
	
}
 
void deleteContact()	
{
 
}
 
void searchContact()
{
 
}
 
void printContact()
{
 
}

Open in new window

pacman32689Asked:
Who is Participating?
 
Jaime OlivaresSoftware ArchitectCommented:
don't use getline() with the date values, look at my example of addContact() function.

Also remove this code block previous to the menu:
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  counter = i;
                  break;
            }
      }
0
 
pacman32689Author Commented:
In other words, would it be like cin.getline(contact[0].name,50)?
0
 
Jaime OlivaresSoftware ArchitectCommented:
yes, it is.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Jaime OlivaresSoftware ArchitectCommented:
Surely you will have some index variable to determine in which struct to write to, then you final application should have something like:

cin.getline(contact[index].name,50) ?
0
 
pacman32689Author Commented:
Yes, I am trying to use the code below to find out what the index is. When I tried to put that variable in the bracket, it gave me hell, even after I passed it as a parameter.
for(int i = 0; i< 250; i++)
        {
                if(!contact[i].validData)
                {
                        counter = i;
                        break;
                }
        }

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
it will be more useful to put this code into a function:

int getFreeCode()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
                  break;
            }
      }
}

also put the structure declaration:
      contactParameter contact[250];
outside the main() function, at the beginning of your program, to make it globally accessible.

then you will need some arrangements:

       switch(menuChoice)
       {
            case 1:
                  addContact(getFreeCode());
                  break;
                                //etcetera
0
 
Jaime OlivaresSoftware ArchitectCommented:
your addContact() need also some refinements:

void addContact(int counter)
{
      cout << "To create a new contact, enter the appropriate information as you are prompted. If you are unsure of something,"
           << " just press enter.\n\n";
      cout << "Name?\n\n";
      cin.getline(contact[counter].name,50);
      cout << "Cellphone number?\n\n";
      cin.getline(contact[counter].cellPhoneNumber,50);
      cout << "Email address?\n\n";
      cin.getline(contact[counter].emailAddress,50);
      cout << "Birthday day?\n\n";
      cin >> contact[counter].birthday.day;      
      cout << "Birthday month?\n\n";
      cin >> contact[counter].birthday.month;      
      cout << "Birthday year?\n\n";
      cin >> contact[counter].birthday.year;      
//      cout << "Contact type?\n\n";
//      cin.getline(contact.contactType,50);     // this wont work and require special handling
//      contact++;   // this is not needed

                contact.validData = true;   // you must set to avoid to be overwritten
}
 
0
 
pacman32689Author Commented:
Getting multiple errors when I try to compile Saying that there are too many arguements. When i try to put up in declaration, it says you cant do that. Help?
#include <iostream>
#include <fstream>
 
using namespace std;
 
 
 
void addContact();
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
int findFreeSpot();
 
struct birthdate
	{
		int date;
		int month;
		int year;
	};
	struct contactParameter
	{
		char name[50];
		char cellPhoneNumber[50];
		char emailAddress[50];
		birthdate birthday;	
		bool validData;
		enum personalOrBusiness
		{
			personal,
			business
		};
		personalOrBusiness pOrBType;
		
	};	
		
int main()
{
	
	string fileName;
	int menuChoice;
	fstream fileData;
	
	int counter = 0;
	
	contactParameter contact[250];
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	cin >> fileName;
	fileData.open(fileName.c_str());
	
	for(int i = 0; i< 250; i++)
	{
		if(!contact[i].validData)
		{
			counter = i;
			break;
		}
	}
	cout << counter;
 
	cout << "Welcome to the address book application. Please select a task from the menu.\n\n"
		 << "1) Add a new contact \n\n2) Update a contact's informantion \n\n3) Delete a contact \n\n"
		 << "4) Search for a contact by name or address \n\n5) Print personal or business contacts in"
		 << " aplphabetical order by name\n\n 6) Exit the Program"; 
		
	 cin >> menuChoice;
	 switch(menuChoice)
	 {
		case 1:
			addContact(findFreeSpot());
			break;
		case 2:
			updateContact();
			break;
		case 3:
			deleteContact();
			break;
		case 4:
			searchContact();
			break;
		case 5:
			printContacts();
			break;
		case 6:
			exit(1);
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			main();
			break;
	 }
}
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
                  break;
            }
      }
}
 
void addContact(int counter)
{
	cout << "To create a new contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just press enter.\n\n";
	cout << "Name?\n\n";
	cin.getline(contact.name,50);
	cout << "Cellphone number?\n\n";
	cin.getline(contact.cellPhoneNumber,50);
	cout << "Email address?\n\n";
	cin.getline(contact.emailAddress,50);
	cout << "Birthday day?\n\n";
	cin.getline(contact.birthday.date,50);
	cout << "Birthday month?\n\n";
	cin.getline(contact.birthday.month,50);
	cout << "Birthday year?\n\n";
	cin.getline(contact.birthday.year,50);
	cout << "Contact type?\n\n";
	cin.getline(contact.contactType,50);
	contact++;
	
	
}
 
void updateContact()
{
	
}
 
void deleteContact()	
{
 
}
 
void searchContact()
{
 
}
 
void printContact()
{
 
}

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
the problem is with the prototype at the beginning of your program, must match the real function:

void updateContact();
should be:
void updateContact(int);

also read my other recommendations carefully
0
 
pacman32689Author Commented:
still getting lots of errors. I will keep checking through. I will also try to find the special handling for enumeration. Can you see what I am missing otherwise?

#include <iostream>
#include <fstream>
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
int findFreeSpot();
 
struct birthdate
	{
		int date;
		int month;
		int year;
	};
	struct contactParameter
	{
		char name[50];
		char cellPhoneNumber[50];
		char emailAddress[50];
		birthdate birthday;	
		bool validData;
		enum personalOrBusiness
		{
			personal,
			business
		};
		personalOrBusiness pOrBType;
		
	};	
		
int main()
{
	
	string fileName;
	int menuChoice;
	fstream fileData;
	
	int counter = 0;
	
	contactParameter contact[250];
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	cin >> fileName;
	fileData.open(fileName.c_str());
	
	findFreeSpot();
	cout << counter;
 
	cout << "Welcome to the address book application. Please select a task from the menu.\n\n"
		 << "1) Add a new contact \n\n2) Update a contact's informantion \n\n3) Delete a contact \n\n"
		 << "4) Search for a contact by name or address \n\n5) Print personal or business contacts in"
		 << " aplphabetical order by name\n\n 6) Exit the Program"; 
		
	 cin >> menuChoice;
	 switch(menuChoice)
	 {
		case 1:
			addContact(findFreeSpot());
			break;
		case 2:
			updateContact();
			break;
		case 3:
			deleteContact();
			break;
		case 4:
			searchContact();
			break;
		case 5:
			printContacts();
			break;
		case 6:
			exit(1);
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			main();
			break;
	 }
}
 
int findFreeSpot(int counter)
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	cout << "To create a new contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just press enter.\n\n";
	cout << "Name?\n\n";
	cin.getline(contact[counter].name,50);
	cout << "Cellphone number?\n\n";
	cin.getline(contact[counter].cellPhoneNumber,50);
	cout << "Email address?\n\n";
	cin.getline(contact[counter].emailAddress,50);
	cout << "Birthday day?\n\n";
	cin.getline(contact[counter].birthday.date,50);
	cout << "Birthday month?\n\n";
	cin.getline(contact[counter].birthday.month,50);
	cout << "Birthday year?\n\n";
	cin.getline(contact[counter].birthday.year,50);
	cout << "Contact type?\n\n";
	//cin.getline(contact[counter].contactType,50);
	
	contact.validData = true;  
}
 
void updateContact()
{
	
}
 
void deleteContact()	
{
 
}
 
void searchContact()
{
 
}
 
void printContact()
{
 
}

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
still you have the struct array inside the main function:

      contactParameter contact[250];

This will cause lots of errors because the other function can't see it. Move it before the main() function.
0
 
pacman32689Author Commented:
im getting int to char conversion errors, how would i convert the ints into chars
0
 
pacman32689Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.