Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use array of structs to store address information

Posted on 2007-12-01
13
Medium Priority
?
204 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:pacman32689
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 

Author Comment

by:pacman32689
ID: 20390176
In other words, would it be like cin.getline(contact[0].name,50)?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20390181
yes, it is.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20390184
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:pacman32689
ID: 20390200
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20390220
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20390230
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
 

Author Comment

by:pacman32689
ID: 20390246
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20390248
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
 

Author Comment

by:pacman32689
ID: 20390283
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20390296
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
 

Author Comment

by:pacman32689
ID: 20390388
im getting int to char conversion errors, how would i convert the ints into chars
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 20390401
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
 

Author Closing Comment

by:pacman32689
ID: 31412162
Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question