Solved

How to use array of structs to store address information

Posted on 2007-12-01
13
194 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
  • 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now