• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Error checking problems, writing to a binary file, and broken function in address book program

Hey experts. I have written an address book program. I tried putting some error checking to check if p or b is actually entered, but it completely fails to work. Also, print function fails to work. When I try to print a contact, nothing happens. Finally, I would like to output the file to a binary file, if someone could point me to a reference, I would highly appreciate it. Thank you so much for your help experts.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
void menu();
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;
		
	};	
	contactParameter contact[250];
	contactParameter tempContactHolder[10];
	
int main()
{
	
	string fileName;
	
	fstream fileData;
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	cin >> fileName;
	fileData.open(fileName.c_str());
	menu();
}	
void menu()
{	
	string str;
	int menuChoice;
	findFreeSpot();
	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\n6) Exit the Program\n\n"; 
		
	 getline (cin,str);
	 menuChoice = atoi (str.c_str());
	 
	 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.";
			menu();
			break;
	 }
	 
}
 
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	char type;
	string str;
	int integer;
	
	cout << "To create a contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just press enter.\n\n";
	cout << "Name?\n\n";
	getline (cin,str);
	strncpy(contact[counter].name, str.c_str(), sizeof(contact[counter].name) - 1);
	contact[counter].name[sizeof(contact[counter].name) - 1] = '\0';
	cout << "Cellphone number?\n\n";
	getline (cin,str);
	strncpy(contact[counter].cellPhoneNumber, str.c_str(), sizeof(contact[counter].cellPhoneNumber) - 1);
	contact[counter].cellPhoneNumber[sizeof(contact[counter].cellPhoneNumber) - 1] = '\0';
	cout << "Email address?\n\n";
	getline (cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	cout << "Birthday day?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.date;
	cout << "Birthday month (number)?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.month;
	cout << "Birthday year?\n\n";
	getline (cin,str);
	integer = atoi(str.c_str());
	integer= contact[counter].birthday.year;
	cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
	getline(cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	type = toupper(type);
	contact[counter].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);      
	contact[counter].validData = true;  
	menu();
	
}
 
void updateContact()
{
	string str;
	int tempModify, modifyMember;
	
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to modify.\n\n";
	getline(cin,str);
	tempModify = atoi (str.c_str());
	cout << "Enter '1' to modify name.\n\nEnter '2' to modify cell phone number.\n\nEnter '3' to modify e-mail address\n\nEnter '4' to modify birthday date\n\nEnter '5' to modify birthday month\n\nEnter "
		 << "'6' to modify birthday year\n\nEnter '7' to modify contact type.\n\n";
	getline(cin,str);
	modifyMember = atoi (str.c_str());
	switch(modifyMember)
	{
		case 1:
			cout << "Name?\n\n";
			cin.getline(contact[tempModify].name,50);
			menu();
			break;
		case 2:
			cout << "Cellphone number?\n\n";
			cin.getline(contact[tempModify].cellPhoneNumber,50);
			menu();
			break;
		case 3:
			cout << "Email address?\n\n";
			cin.getline(contact[tempModify].emailAddress,50);
			menu();
			break;
		case 4:
			cout << "Birthday day?\n\n";
			cin >> contact[tempModify].birthday.date;
			menu();
			break;
		case 5:
			cout << "Birthday month (number)?\n\n";
			cin >> contact[tempModify].birthday.month;
			menu();
			break;
		case 6:
			char type;
			while( type!= 'p' || type != 'b')
			{
				cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
				cin >> type;
				type = toupper(type);
			}
			contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business); 
			menu();     
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			updateContact();
 
	}		
 
}
 
void deleteContact()	
{
	string str;
	int numDelete;
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to delete.\n\n";
	getline(cin,str);
	numDelete = atoi (str.c_str());
	contact[numDelete].validData = false;
	menu();
}
 
void searchContact()
{
	
	int temp;
	string str;
	
	while(temp!= 0 || temp!= 1)
	{
		cout << "Please enter a '0' if you are searching by name or a '1' if you are searching by e-mail address\n\n";
		getline (cin,str);
		temp = atoi (str.c_str());
	}
	if(temp == 0)
	{
		cout << "Please enter the name you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].name,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	if(temp == 1)
	{
		cout << "Please enter the e-mail address you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].emailAddress,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	
	menu();				
}
 
void printContacts()
{
	int tempPrint;
	string str;
	bool swapped = true;
	
	while(tempPrint!= 0 || tempPrint!= 1)
	{
		cout << "Please enter 0 if you would like to view personal contacts or 1 if you would like to view business contacts.";
		getline(cin,str);
		tempPrint = atoi (str.c_str());
	}	
	
	if(tempPrint = 0)
	{
		
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 0)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << "Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	if(tempPrint = 1)
	{
		swapped = true;
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 1)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << i << " Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	menu();
 
}

Open in new window

0
pacman32689
Asked:
pacman32689
  • 12
  • 5
  • 2
4 Solutions
 
Jaime OlivaresCommented:
I have noticed this in the addContact() function:
{
   // some initial code
      menu();   // do not call menu here recursivelly, just remove this line
}

about the updateContact:

            case 6:
                  char type;
                  //while( type!= 'p' || type != 'b')
                                                do    // a do...while loop will be better here
                  {
                        cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
                        cin >> type;
                        type = toupper(type);
                  } while( type!= 'P' || type != 'B')  // p and b should be uppercase
                  contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);
                  menu();// don't call menu, just return    
                  break;
0
 
jkrCommented:
Your 'printContacts()' does not work as expected for a simple reason:

      if(tempPrint = 0)

and

      if(tempPrint = 1)

Use '==' for both statements.
0
 
jkrCommented:
Also, check line 146. In 'type = toupper(type);', 'type' never was initialzed before.
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.

 
pacman32689Author Commented:
Ok, I made the changes, but still no effect. When I add contacts, it is still accepting chars other that P or B

When I tried to do return instead of recursion, I got errors telling me I can't do that in a void function.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
void menu();
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;
		
	};	
	contactParameter contact[250];
	contactParameter tempContactHolder[10];
	
int main()
{
	
	string fileName;
	
	fstream fileData;
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	cin >> fileName;
	fileData.open(fileName.c_str());
	menu();
}	
void menu()
{	
	string str;
	int menuChoice;
	findFreeSpot();
	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\n6) Exit the Program\n\n"; 
		
	 getline (cin,str);
	 menuChoice = atoi (str.c_str());
	 
	 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.";
			menu();
			break;
	 }
	 
}
 
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	char type;
	string str;
	int integer;
	
	cout << "To create a contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just press enter.\n\n";
	cout << "Name?\n\n";
	getline (cin,str);
	strncpy(contact[counter].name, str.c_str(), sizeof(contact[counter].name) - 1);
	contact[counter].name[sizeof(contact[counter].name) - 1] = '\0';
	cout << "Cellphone number?\n\n";
	getline (cin,str);
	strncpy(contact[counter].cellPhoneNumber, str.c_str(), sizeof(contact[counter].cellPhoneNumber) - 1);
	contact[counter].cellPhoneNumber[sizeof(contact[counter].cellPhoneNumber) - 1] = '\0';
	cout << "Email address?\n\n";
	getline (cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	cout << "Birthday day?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.date;
	cout << "Birthday month (number)?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.month;
	cout << "Birthday year?\n\n";
	getline (cin,str);
	integer = atoi(str.c_str());
	integer= contact[counter].birthday.year;
	cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
	getline(cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	//type = toupper(type);
	contact[counter].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);      
	contact[counter].validData = true;  
	menu();
	
}
 
void updateContact()
{
	string str;
	int tempModify, modifyMember;
	
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to modify.\n\n";
	getline(cin,str);
	tempModify = atoi (str.c_str());
	cout << "Enter '1' to modify name.\n\nEnter '2' to modify cell phone number.\n\nEnter '3' to modify e-mail address\n\nEnter '4' to modify birthday date\n\nEnter '5' to modify birthday month\n\nEnter "
		 << "'6' to modify birthday year\n\nEnter '7' to modify contact type.\n\n";
	getline(cin,str);
	modifyMember = atoi (str.c_str());
	switch(modifyMember)
	{
		case 1:
			cout << "Name?\n\n";
			cin.getline(contact[tempModify].name,50);
			menu();
			break;
		case 2:
			cout << "Cellphone number?\n\n";
			cin.getline(contact[tempModify].cellPhoneNumber,50);
			menu();
			break;
		case 3:
			cout << "Email address?\n\n";
			cin.getline(contact[tempModify].emailAddress,50);
			menu();
			break;
		case 4:
			cout << "Birthday day?\n\n";
			cin >> contact[tempModify].birthday.date;
			menu();
			break;
		case 5:
			cout << "Birthday month (number)?\n\n";
			cin >> contact[tempModify].birthday.month;
			menu();
			break;
		case 6:
			char type;
			do
			{
				cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
				cin >> type;
				type = toupper(type);
			}while(type!='P' || type!='B');
			contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business); 
			menu(); 
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			updateContact();
 
	}		
 
}
 
void deleteContact()	
{
	string str;
	int numDelete;
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to delete.\n\n";
	getline(cin,str);
	numDelete = atoi (str.c_str());
	contact[numDelete].validData = false;
	menu();
}
 
void searchContact()
{
	
	int temp;
	string str;
	
	while(temp!= 0 || temp!= 1)
	{
		cout << "Please enter a '0' if you are searching by name or a '1' if you are searching by e-mail address\n\n";
		getline (cin,str);
		temp = atoi (str.c_str());
	}
	if(temp == 0)
	{
		cout << "Please enter the name you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].name,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	if(temp == 1)
	{
		cout << "Please enter the e-mail address you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].emailAddress,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	
	menu();				
}
 
void printContacts()
{
	int tempPrint;
	string str;
	bool swapped = true;
	
	do	{
		cout << "Please enter 0 if you would like to view personal contacts or 1 if you would like to view business contacts.";
		getline(cin,str);
		tempPrint = atoi (str.c_str());
	}while(tempPrint!= 0 || tempPrint!= 1);
 
	 
	if(tempPrint == 0)
	{
		
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 0)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << "Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	if(tempPrint == 1)
	{
		swapped = true;
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 1)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << i << " Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	menu();
 
}

Open in new window

0
 
pacman32689Author Commented:
Made a few more mods, I am trying to understand how to read a previous address book into my program. I am assuming I will write an array of structs into a file, but other than that, I am a bit perplexed on how to assign structs in the array to an array in the program.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
void menu();
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;
		
	};	
	contactParameter contact[250];
	contactParameter tempContactHolder[10];
	
int main()
{
	
	string fileName;
	
	fstream fileData;
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	cin >> fileName;
	fileData.open(fileName.c_str(), ios::in | ios::binary);
	menu();
}	
void menu()
{	
	string str;
	int menuChoice;
	findFreeSpot();
	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\n6) Exit the Program\n\n"; 
		
	 getline (cin,str);
	 menuChoice = atoi (str.c_str());
	 
	 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.";
			menu();
			break;
	 }
	 
}
 
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	char type;
	string str;
	int integer;
	
	cout << "To create a contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just press enter.\n\n";
	cout << "Name?\n\n";
	getline (cin,str);
	strncpy(contact[counter].name, str.c_str(), sizeof(contact[counter].name) - 1);
	contact[counter].name[sizeof(contact[counter].name) - 1] = '\0';
	cout << "Cellphone number?\n\n";
	getline (cin,str);
	strncpy(contact[counter].cellPhoneNumber, str.c_str(), sizeof(contact[counter].cellPhoneNumber) - 1);
	contact[counter].cellPhoneNumber[sizeof(contact[counter].cellPhoneNumber) - 1] = '\0';
	cout << "Email address?\n\n";
	getline (cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	cout << "Birthday day?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.date;
	cout << "Birthday month (number)?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.month;
	cout << "Birthday year?\n\n";
	getline (cin,str);
	integer = atoi(str.c_str());
	integer= contact[counter].birthday.year;
	cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
	getline(cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	//type = toupper(type);
	contact[counter].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);      
	contact[counter].validData = true;  
	menu();
	
}
 
void updateContact()
{
	string str;
	int tempModify, modifyMember;
	
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to modify.\n\n";
	getline(cin,str);
	tempModify = atoi (str.c_str());
	cout << "Enter '1' to modify name.\n\nEnter '2' to modify cell phone number.\n\nEnter '3' to modify e-mail address\n\nEnter '4' to modify birthday date\n\nEnter '5' to modify birthday month\n\nEnter "
		 << "'6' to modify birthday year\n\nEnter '7' to modify contact type.\n\n";
	getline(cin,str);
	modifyMember = atoi (str.c_str());
	switch(modifyMember)
	{
		case 1:
			cout << "Name?\n\n";
			cin.getline(contact[tempModify].name,50);
			menu();
			break;
		case 2:
			cout << "Cellphone number?\n\n";
			cin.getline(contact[tempModify].cellPhoneNumber,50);
			menu();
			break;
		case 3:
			cout << "Email address?\n\n";
			cin.getline(contact[tempModify].emailAddress,50);
			menu();
			break;
		case 4:
			cout << "Birthday day?\n\n";
			cin >> contact[tempModify].birthday.date;
			menu();
			break;
		case 5:
			cout << "Birthday month (number)?\n\n";
			cin >> contact[tempModify].birthday.month;
			menu();
			break;
		case 6:
			char type;
			do
			{
				cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
				cin >> type;
				type = toupper(type);
			}while(type!='P' || type!='B');
			contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business); 
			menu(); 
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			updateContact();
 
	}		
 
}
 
void deleteContact()	
{
	string str;
	int numDelete;
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to delete.\n\n";
	getline(cin,str);
	numDelete = atoi (str.c_str());
	contact[numDelete].validData = false;
	menu();
}
 
void searchContact()
{
	
	int temp;
	string str;
	
	while(temp!= 0 || temp!= 1)
	{
		cout << "Please enter a '0' if you are searching by name or a '1' if you are searching by e-mail address\n\n";
		getline (cin,str);
		temp = atoi (str.c_str());
	}
	if(temp == 0)
	{
		cout << "Please enter the name you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].name,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	if(temp == 1)
	{
		cout << "Please enter the e-mail address you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].emailAddress,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	
	menu();				
}
 
void printContacts()
{
	int tempPrint;
	string str;
	bool swapped = true;
	
	do	{
		cout << "Please enter 0 if you would like to view personal contacts or 1 if you would like to view business contacts.";
		getline(cin,str);
		tempPrint = atoi (str.c_str());
	}while(tempPrint!= 0 || tempPrint!= 1);
 
	 
	if(tempPrint == 0)
	{
		
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 0)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << "Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	if(tempPrint == 1)
	{
		swapped = true;
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 1)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << i << " Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	menu();
 
}

Open in new window

0
 
Jaime OlivaresCommented:
this line:
     }while(type!='P' || type!='B');
should be:
     }while(type!='P' && type!='B');
0
 
pacman32689Author Commented:
hmm, I can't enter any char now.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
void menu();
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;
		
	};	
	contactParameter contact[250];
	contactParameter tempContactHolder[10];
	
int main()
{
	string str;
	string fileName;
	
	fstream fileData;
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	getline(cin,fileName);
	fileData.open(fileName.c_str(), ios::in | ios::binary);
	menu();
}	
void menu()
{	
	string str;
	int menuChoice;
	findFreeSpot();
	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\n6) Exit the Program\n\n"; 
		
	 getline (cin,str);
	 menuChoice = atoi (str.c_str());
	 
	 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.";
		//	menu();
			break;
	 }
	 
}
 
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	char type;
	string str;
	int integer;
	
	cout << "To create a contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just enter a -1.\n\n";
	cout << "Name?\n\n";
	getline (cin,str);
	strncpy(contact[counter].name, str.c_str(), sizeof(contact[counter].name) - 1);
	contact[counter].name[sizeof(contact[counter].name) - 1] = '\0';
	cout << "Cellphone number?\n\n";
	getline (cin,str);
	strncpy(contact[counter].cellPhoneNumber, str.c_str(), sizeof(contact[counter].cellPhoneNumber) - 1);
	contact[counter].cellPhoneNumber[sizeof(contact[counter].cellPhoneNumber) - 1] = '\0';
	cout << "Email address?\n\n";
	getline (cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	cout << "Birthday day?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.date;
	cout << "Birthday month (number)?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.month;
	cout << "Birthday year?\n\n";
	getline (cin,str);
	integer = atoi(str.c_str());
	integer= contact[counter].birthday.year;
	do
	{
		cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
		getline(cin,str);
		strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
		contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	}while(type!='P' && type!='B');
	contact[counter].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);      
	contact[counter].validData = true;  
	menu();
	
}
 
void updateContact()
{
	string str;
	int tempModify, modifyMember;
	
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to modify.\n\n";
	getline(cin,str);
	tempModify = atoi (str.c_str());
	cout << "Enter '1' to modify name.\n\nEnter '2' to modify cell phone number.\n\nEnter '3' to modify e-mail address\n\nEnter '4' to modify birthday date\n\nEnter '5' to modify birthday month\n\nEnter "
		 << "'6' to modify birthday year\n\nEnter '7' to modify contact type.\n\n";
	getline(cin,str);
	modifyMember = atoi (str.c_str());
	switch(modifyMember)
	{
		case 1:
			cout << "Name?\n\n";
			cin.getline(contact[tempModify].name,50);
			menu();
			break;
		case 2:
			cout << "Cellphone number?\n\n";
			cin.getline(contact[tempModify].cellPhoneNumber,50);
			menu();
			break;
		case 3:
			cout << "Email address?\n\n";
			cin.getline(contact[tempModify].emailAddress,50);
			menu();
			break;
		case 4:
			cout << "Birthday day?\n\n";
			cin >> contact[tempModify].birthday.date;
			menu();
			break;
		case 5:
			cout << "Birthday month (number)?\n\n";
			cin >> contact[tempModify].birthday.month;
			menu();
			break;
		case 6:
			char type;
			do
			{
				cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
				cin >> type;
			}while(type!='P' && type!='B');
			contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business); 
			menu(); 
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			updateContact();
 
	}		
 
}
 
void deleteContact()	
{
	string str;
	int numDelete;
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to delete.\n\n";
	getline(cin,str);
	numDelete = atoi (str.c_str());
	contact[numDelete].validData = false;
	menu();
}
 
void searchContact()
{
	
	int temp;
	string str;
	
	while(temp!= 0 || temp!= 1)
	{
		cout << "Please enter a '0' if you are searching by name or a '1' if you are searching by e-mail address\n\n";
		getline (cin,str);
		temp = atoi (str.c_str());
	}
	if(temp == 0)
	{
		cout << "Please enter the name you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].name,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	if(temp == 1)
	{
		cout << "Please enter the e-mail address you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].emailAddress,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	
	menu();				
}
 
void printContacts()
{
	int tempPrint;
	string str;
	bool swapped = true;
	
	do	{
		cout << "Please enter 0 if you would like to view personal contacts or 1 if you would like to view business contacts.";
		getline(cin,str);
		tempPrint = atoi (str.c_str());
	}while(tempPrint!= 0 || tempPrint!= 1);
 
	 
	if(tempPrint == 0)
	{
		
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 0)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << "Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	if(tempPrint == 1)
	{
		swapped = true;
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 1)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << i << " Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	menu();
 
}

Open in new window

0
 
pacman32689Author Commented:
It might be because I didn't use a getline, fixing.
0
 
pacman32689Author Commented:
Made further modifications, no luck still. Anyone see what I am missing? >.<
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
void menu();
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;
		
	};	
	contactParameter contact[250];
	contactParameter tempContactHolder[10];
	
int main()
{
	string str;
	string fileName;
	
	fstream fileData;
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	getline(cin,fileName);
	fileData.open(fileName.c_str(), ios::in | ios::binary);
	menu();
}	
void menu()
{	
	string str;
	int menuChoice;
	findFreeSpot();
	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\n6) Exit the Program\n\n"; 
		
	 getline (cin,str);
	 menuChoice = atoi (str.c_str());
	 
	 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.";
			menu();
			break;
	 }
	 
}
 
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	char type;
	string str;
	int integer;
	
	cout << "To create a contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just enter a -1.\n\n";
	cout << "Name?\n\n";
	getline (cin,str);
	strncpy(contact[counter].name, str.c_str(), sizeof(contact[counter].name) - 1);
	contact[counter].name[sizeof(contact[counter].name) - 1] = '\0';
	cout << "Cellphone number?\n\n";
	getline (cin,str);
	strncpy(contact[counter].cellPhoneNumber, str.c_str(), sizeof(contact[counter].cellPhoneNumber) - 1);
	contact[counter].cellPhoneNumber[sizeof(contact[counter].cellPhoneNumber) - 1] = '\0';
	cout << "Email address?\n\n";
	getline (cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	cout << "Birthday day?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.date;
	cout << "Birthday month (number)?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.month;
	cout << "Birthday year?\n\n";
	getline (cin,str);
	integer = atoi(str.c_str());
	integer= contact[counter].birthday.year;
	do
	{
		cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
		getline(cin,str);
		strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
		contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	}while(type!='P' && type!='B');
	contact[counter].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);      
	contact[counter].validData = true;  
	menu();
	
}
 
void updateContact()
{
	string str;
	int integer;
	int tempModify, modifyMember;
	
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to modify.\n\n";
	getline(cin,str);
	tempModify = atoi (str.c_str());
	cout << "Enter '1' to modify name.\n\nEnter '2' to modify cell phone number.\n\nEnter '3' to modify e-mail address\n\nEnter '4' to modify birthday date\n\nEnter '5' to modify birthday month\n\nEnter "
		 << "'6' to modify birthday year\n\nEnter '7' to modify contact type.\n\n";
	getline(cin,str);
	modifyMember = atoi (str.c_str());
	switch(modifyMember)
	{
		case 1:
			cout << "Name?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].name, str.c_str(), sizeof(contact[tempModify].name) - 1);
			contact[tempModify].name[sizeof(contact[tempModify].name) - 1] = '\0';
			menu();
			break;
		case 2:
			cout << "Cellphone number?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].cellPhoneNumber, str.c_str(), sizeof(contact[tempModify].cellPhoneNumber) - 1);
			contact[tempModify].cellPhoneNumber[sizeof(contact[tempModify].cellPhoneNumber) - 1] = '\0';
			menu();
			break;
		case 3:
			cout << "Email address?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].emailAddress, str.c_str(), sizeof(contact[tempModify].emailAddress) - 1);
			contact[tempModify].emailAddress[sizeof(contact[tempModify].emailAddress) - 1] = '\0';
			menu();
			break;
		case 4:
			cout << "Birthday month (number)?\n\n";
			getline (cin,str);
			integer = atoi (str.c_str());
			integer = contact[tempModify].birthday.month;
			menu();
			break;
		case 5:
			cout << "Birthday year?\n\n";
			getline (cin,str);
			integer = atoi(str.c_str());
			integer= contact[tempModify].birthday.year;			menu();
			break;
		case 6:
			char type;
			do
			{
				cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
				getline(cin,str);
				strncpy(contact[tempModify].emailAddress, str.c_str(), sizeof(contact[tempModify].emailAddress) - 1);
				contact[tempModify].emailAddress[sizeof(contact[tempModify].emailAddress) - 1] = '\0';
 
			}while(type!='P' && type!='B');
			contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business); 
			menu(); 
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			updateContact();
			break;
 
	}		
 
}
 
void deleteContact()	
{
	string str;
	int numDelete;
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to delete.\n\n";
	getline(cin,str);
	numDelete = atoi (str.c_str());
	contact[numDelete].validData = false;
	menu();
}
 
void searchContact()
{
	
	int temp;
	string str;
	
	while(temp!= 0 || temp!= 1)
	{
		cout << "Please enter a '0' if you are searching by name or a '1' if you are searching by e-mail address\n\n";
		getline (cin,str);
		temp = atoi (str.c_str());
	}
	if(temp == 0)
	{
		cout << "Please enter the name you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].name,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	if(temp == 1)
	{
		cout << "Please enter the e-mail address you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].emailAddress,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	
	menu();				
}
 
void printContacts()
{
	int tempPrint;
	string str;
	bool swapped = true;
	
	do	{
		cout << "Please enter 0 if you would like to view personal contacts or 1 if you would like to view business contacts.";
		getline(cin,str);
		tempPrint = atoi (str.c_str());
	}while(tempPrint!= 0 || tempPrint!= 1);
 
	 
	if(tempPrint == 0)
	{
		
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 0)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << "Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	if(tempPrint == 1)
	{
		swapped = true;
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 1)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << i << " Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	menu();
 
}

Open in new window

0
 
Jaime OlivaresCommented:
what happened with the first code snippet? your current while loop will never work becuase you are not giving 'type' some value:

                 char type;
                  {
                        cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
                        cin >> type;
                        type = toupper(type);
                  } while( type!= 'P' && type != 'B')  // p and b should be uppercase
 
0
 
pacman32689Author Commented:
Dumb mistake on my part mispasting:) Hm, now when I try to modify type in updateContact it isn't working. Will check it out.
0
 
pacman32689Author Commented:
Found it, forgot to add in birthday date. Fixing
0
 
pacman32689Author Commented:
Mmm, my print function is not working. When I accept user input, it just returns to the menu
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
void menu();
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;
		
	};	
	contactParameter contact[250];
	contactParameter tempContactHolder[10];
	
int main()
{
	string str;
	string fileName;
	
	fstream fileData;
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	getline(cin,fileName);
	fileData.open(fileName.c_str(), ios::in | ios::binary);
	menu();
}	
void menu()
{	
	string str;
	int menuChoice;
	findFreeSpot();
	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\n6) Exit the Program\n\n"; 
		
	 getline (cin,str);
	 menuChoice = atoi (str.c_str());
	 
	 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.";
			menu();
			break;
	 }
	 
}
 
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	char type;
	string str;
	int integer;
	
	cout << "To create a contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just enter a -1.\n\n";
	cout << "Name?\n\n";
	getline (cin,str);
	strncpy(contact[counter].name, str.c_str(), sizeof(contact[counter].name) - 1);
	contact[counter].name[sizeof(contact[counter].name) - 1] = '\0';
	cout << "Cellphone number?\n\n";
	getline (cin,str);
	strncpy(contact[counter].cellPhoneNumber, str.c_str(), sizeof(contact[counter].cellPhoneNumber) - 1);
	contact[counter].cellPhoneNumber[sizeof(contact[counter].cellPhoneNumber) - 1] = '\0';
	cout << "Email address?\n\n";
	getline (cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	cout << "Birthday day?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.date;
	cout << "Birthday month (number)?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.month;
	cout << "Birthday year?\n\n";
	getline (cin,str);
	integer = atoi(str.c_str());
	integer= contact[counter].birthday.year;
	do
	{
		cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
		cin >> type;
	}while(type!='P' && type!='B');
	contact[counter].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);      
	contact[counter].validData = true;  
	menu();
	
}
 
void updateContact()
{
	string str;
	int integer;
	int tempModify, modifyMember;
	
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to modify.\n\n";
	getline(cin,str);
	tempModify = atoi (str.c_str());
	cout << "Enter '1' to modify name.\n\nEnter '2' to modify cell phone number.\n\nEnter '3' to modify e-mail address\n\nEnter '4' to modify birthday date\n\nEnter '5' to modify birthday month\n\nEnter "
		 << "'6' to modify birthday year\n\nEnter '7' to modify contact type.\n\n";
	getline(cin,str);
	modifyMember = atoi (str.c_str());
	switch(modifyMember)
	{
		case 1:
			cout << "Name?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].name, str.c_str(), sizeof(contact[tempModify].name) - 1);
			contact[tempModify].name[sizeof(contact[tempModify].name) - 1] = '\0';
			menu();
			break;
		case 2:
			cout << "Cellphone number?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].cellPhoneNumber, str.c_str(), sizeof(contact[tempModify].cellPhoneNumber) - 1);
			contact[tempModify].cellPhoneNumber[sizeof(contact[tempModify].cellPhoneNumber) - 1] = '\0';
			menu();
			break;
		case 3:
			cout << "Email address?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].emailAddress, str.c_str(), sizeof(contact[tempModify].emailAddress) - 1);
			contact[tempModify].emailAddress[sizeof(contact[tempModify].emailAddress) - 1] = '\0';
			menu();
			break;
		case 4:
			cout <<"Birthday day? (number)?\n\n";
			getline (cin,str);
			integer = atoi (str.c_str());
			integer = contact[tempModify].birthday.date;
			menu();
			break;
 
		case 5:
			cout << "Birthday month (number)?\n\n";
			getline (cin,str);
			integer = atoi (str.c_str());
			integer = contact[tempModify].birthday.month;
			menu();
			break;
		case 6:
			cout << "Birthday year?\n\n";
			getline (cin,str);
			integer = atoi(str.c_str());
			integer= contact[tempModify].birthday.year;	
			menu();
			break;
		case 7:
			char type;
			do
			{
				cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
				cin >> type;
			}while(type!='P' && type!='B');
			contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business); 
			menu(); 
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your menu choice.";
			updateContact();
			break;
 
	}		
 
}
 
void deleteContact()	
{
	string str;
	int numDelete;
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to delete.\n\n";
	getline(cin,str);
	numDelete = atoi (str.c_str());
	contact[numDelete].validData = false;
	menu();
}
 
void searchContact()
{
	
	int temp;
	string str;
	
	while(temp!= 0 || temp!= 1)
	{
		cout << "Please enter a '0' if you are searching by name or a '1' if you are searching by e-mail address\n\n";
		getline (cin,str);
		temp = atoi (str.c_str());
	}
	if(temp == 0)
	{
		cout << "Please enter the name you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].name,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	if(temp == 1)
	{
		cout << "Please enter the e-mail address you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].emailAddress,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
				menu();
			}
		}	
			
	}
	
	menu();				
}
 
void printContacts()
{
	int tempPrint;
	string str;
	bool swapped = true;
	
	do	{
			cout << "Please enter 0 if you would like to view personal contacts or 1 if you would like to view business contacts.";
			getline(cin,str);
			tempPrint = atoi (str.c_str());
	}while(tempPrint!= 0 && tempPrint!= 1);
 
	 
	if(tempPrint == 0)
	{
		
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 0)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << "Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	if(tempPrint == 1)
	{
		swapped = true;
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 1)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << i << " Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	menu();
 
}

Open in new window

0
 
pacman32689Author Commented:
Also, I am going to need to be able to read in a file of these and write one at closing of the program. How would I go about this? Is there some way to diving each struct with a '&' and each member by a : ?
0
 
Jaime OlivaresCommented:
start removing all invocations to menu() in your functions. this generates undesired recursive calls to the function, you just need an infinite loop in your main() function:

int main()
{
      string str;
      string fileName;
      fstream fileData;
      
      cout << "Please enter the name of the file holding your address book.\n\n";
      getline(cin,fileName);
      fileData.open(fileName.c_str(), ios::in | ios::binary);

                while (true)  // infinite loop
                {
              menu();
                }
}
0
 
pacman32689Author Commented:
Ok, I cleaned up my code. Any idea about the others logic problems I am having :)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
void menu();
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;
		
	};	
	contactParameter contact[250];
	contactParameter tempContactHolder[10];
	
int main()
{
	string str;
	string fileName;
	
	fstream fileData;
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	getline(cin,fileName);
	fileData.open(fileName.c_str(), ios::in | ios::binary);
	while (true)  // infinite loop
	{
		menu();
	}
}	
void menu()
{	
	string str;
	int menuChoice;
	findFreeSpot();
	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\n6) Exit the Program\n\n"; 
		
	 getline (cin,str);
	 menuChoice = atoi (str.c_str());
	 
	 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.";
			break;
	 }
	 
}
 
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	char type;
	string str;
	int integer;
	
	cout << "To create a contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just enter a -1.\n\n";
	cout << "Name?\n\n";
	getline (cin,str);
	strncpy(contact[counter].name, str.c_str(), sizeof(contact[counter].name) - 1);
	contact[counter].name[sizeof(contact[counter].name) - 1] = '\0';
	cout << "Cellphone number?\n\n";
	getline (cin,str);
	strncpy(contact[counter].cellPhoneNumber, str.c_str(), sizeof(contact[counter].cellPhoneNumber) - 1);
	contact[counter].cellPhoneNumber[sizeof(contact[counter].cellPhoneNumber) - 1] = '\0';
	cout << "Email address?\n\n";
	getline (cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	cout << "Birthday day?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.date;
	cout << "Birthday month (number)?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.month;
	cout << "Birthday year?\n\n";
	getline (cin,str);
	integer = atoi(str.c_str());
	integer= contact[counter].birthday.year;
	do
	{
		cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
		cin >> type;
	}while(type!='P' && type!='B');
	contact[counter].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);      
	contact[counter].validData = true;  
 
	
}
 
void updateContact()
{
	string str;
	int integer;
	int tempModify, modifyMember;
	
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to modify.\n\n";
	getline(cin,str);
	tempModify = atoi (str.c_str());
	cout << "Enter '1' to modify name.\n\nEnter '2' to modify cell phone number.\n\nEnter '3' to modify e-mail address\n\nEnter '4' to modify birthday date\n\nEnter '5' to modify birthday month\n\nEnter "
		 << "'6' to modify birthday year\n\nEnter '7' to modify contact type.\n\n";
	getline(cin,str);
	modifyMember = atoi (str.c_str());
	switch(modifyMember)
	{
		case 1:
			cout << "Name?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].name, str.c_str(), sizeof(contact[tempModify].name) - 1);
			contact[tempModify].name[sizeof(contact[tempModify].name) - 1] = '\0';
			break;
		case 2:
			cout << "Cellphone number?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].cellPhoneNumber, str.c_str(), sizeof(contact[tempModify].cellPhoneNumber) - 1);
			contact[tempModify].cellPhoneNumber[sizeof(contact[tempModify].cellPhoneNumber) - 1] = '\0';
			break;
		case 3:
			cout << "Email address?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].emailAddress, str.c_str(), sizeof(contact[tempModify].emailAddress) - 1);
			contact[tempModify].emailAddress[sizeof(contact[tempModify].emailAddress) - 1] = '\0';
			break;
		case 4:
			cout <<"Birthday day? (number)?\n\n";
			getline (cin,str);
			integer = atoi (str.c_str());
			integer = contact[tempModify].birthday.date;
			break;
 
		case 5:
			cout << "Birthday month (number)?\n\n";
			getline (cin,str);
			integer = atoi (str.c_str());
			integer = contact[tempModify].birthday.month;
			break;
		case 6:
			cout << "Birthday year?\n\n";
			getline (cin,str);
			integer = atoi(str.c_str());
			integer= contact[tempModify].birthday.year;	
			break;
		case 7:
			char type;
			do
			{
				cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
				cin >> type;
			}while(type!='P' && type!='B');
			contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business); 
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your choice.";
			updateContact();
			break;
 
	}		
 
}
 
void deleteContact()	
{
	string str;
	int numDelete;
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to delete.\n\n";
	getline(cin,str);
	numDelete = atoi (str.c_str());
	contact[numDelete].validData = false;
}
 
void searchContact()
{
	
	int temp;
	string str;
	
	while(temp!= 0 || temp!= 1)
	{
		cout << "Please enter a '0' if you are searching by name or a '1' if you are searching by e-mail address\n\n";
		getline (cin,str);
		temp = atoi (str.c_str());
	}
	if(temp == 0)
	{
		cout << "Please enter the name you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].name,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
			}
		}	
			
	}
	if(temp == 1)
	{
		cout << "Please enter the e-mail address you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].emailAddress,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
			}
		}	
			
	}
		
}
 
void printContacts()
{
	int tempPrint;
	string str;
	bool swapped = true;
	
	do	{
			cout << "Please enter 0 if you would like to view personal contacts or 1 if you would like to view business contacts.";
//			getline(cin,str);
//			tempPrint = atoi (str.c_str());
			cin >> tempPrint;
	}while(tempPrint!= 0 || tempPrint!= 1);
 
	 
	if(tempPrint == 0)
	{
		
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 0)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << "Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	if(tempPrint == 1)
	{
		swapped = true;
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 1)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << i << " Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
 
}

Open in new window

0
 
pacman32689Author Commented:
fixed! Hm, I have never written an array of structs to a file before, anyone have a resource that tells a way, or is writing structs to a binary file the same as any other data type?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
 
 
void addContact(int counter);
void updateContact();
void deleteContact();
void printContacts();
void searchContact();
void menu();
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;
		
	};	
	contactParameter contact[250];
	contactParameter tempContactHolder[10];
	
int main()
{
	string str;
	string fileName;
	
	fstream fileData;
	
	cout << "Please enter the name of the file holding your address book.\n\n";
	getline(cin,fileName);
	fileData.open(fileName.c_str(), ios::in | ios::binary);
	while (true)  // infinite loop to keep returning to menu	
	{
		menu();
	}
}	
void menu()
{	
	string str;
	int menuChoice;
	findFreeSpot();
	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"
		 << " alphabetical order by name\n\n6) Exit the Program\n\n"; 
		
	 getline (cin,str);
	 menuChoice = atoi (str.c_str());
	 
	 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.";
			break;
	 }
	 
}
 
 
int findFreeSpot()
{
      for(int i = 0; i< 250; i++)
      {
            if(!contact[i].validData)
            {
                  return i;
 
            }
      }
}
 
void addContact(int counter)
{
	char type;
	string str;
	int integer;
	
	cout << "To create a contact, enter the appropriate information as you are prompted. If you are unsure of something,"
	     << " just enter a -1.\n\n";
	cout << "Name?\n\n";
	getline (cin,str);
	strncpy(contact[counter].name, str.c_str(), sizeof(contact[counter].name) - 1);
	contact[counter].name[sizeof(contact[counter].name) - 1] = '\0';
	cout << "Cellphone number?\n\n";
	getline (cin,str);
	strncpy(contact[counter].cellPhoneNumber, str.c_str(), sizeof(contact[counter].cellPhoneNumber) - 1);
	contact[counter].cellPhoneNumber[sizeof(contact[counter].cellPhoneNumber) - 1] = '\0';
	cout << "Email address?\n\n";
	getline (cin,str);
	strncpy(contact[counter].emailAddress, str.c_str(), sizeof(contact[counter].emailAddress) - 1);
	contact[counter].emailAddress[sizeof(contact[counter].emailAddress) - 1] = '\0';
	cout << "Birthday day?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.date;
	cout << "Birthday month (number)?\n\n";
	getline (cin,str);
	integer = atoi (str.c_str());
	integer = contact[counter].birthday.month;
	cout << "Birthday year?\n\n";
	getline (cin,str);
	integer = atoi(str.c_str());
	integer= contact[counter].birthday.year;
	do
	{
		cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
		cin >> type;
	}while(type!='P' && type!='B');
	contact[counter].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business);      
	contact[counter].validData = true;  
 
	
}
 
void updateContact()
{
	string str;
	int integer;
	int tempModify, modifyMember;
	
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to modify.\n\n";
	getline(cin,str);
	tempModify = atoi (str.c_str());
	cout << "Enter '1' to modify name.\n\nEnter '2' to modify cell phone number.\n\nEnter '3' to modify e-mail address\n\nEnter '4' to modify birthday date\n\nEnter '5' to modify birthday month\n\nEnter "
		 << "'6' to modify birthday year\n\nEnter '7' to modify contact type.\n\n";
	getline(cin,str);
	modifyMember = atoi (str.c_str());
	switch(modifyMember)
	{
		case 1:
			cout << "Name?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].name, str.c_str(), sizeof(contact[tempModify].name) - 1);
			contact[tempModify].name[sizeof(contact[tempModify].name) - 1] = '\0';
			break;
		case 2:
			cout << "Cellphone number?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].cellPhoneNumber, str.c_str(), sizeof(contact[tempModify].cellPhoneNumber) - 1);
			contact[tempModify].cellPhoneNumber[sizeof(contact[tempModify].cellPhoneNumber) - 1] = '\0';
			break;
		case 3:
			cout << "Email address?\n\n";
			getline (cin,str);
			strncpy(contact[tempModify].emailAddress, str.c_str(), sizeof(contact[tempModify].emailAddress) - 1);
			contact[tempModify].emailAddress[sizeof(contact[tempModify].emailAddress) - 1] = '\0';
			break;
		case 4:
			cout <<"Birthday day? (number)?\n\n";
			getline (cin,str);
			integer = atoi (str.c_str());
			integer = contact[tempModify].birthday.date;
			break;
 
		case 5:
			cout << "Birthday month (number)?\n\n";
			getline (cin,str);
			integer = atoi (str.c_str());
			integer = contact[tempModify].birthday.month;
			break;
		case 6:
			cout << "Birthday year?\n\n";
			getline (cin,str);
			integer = atoi(str.c_str());
			integer= contact[tempModify].birthday.year;	
			break;
		case 7:
			char type;
			do
			{
				cout << "Contact type is (P)ersonal or (B)usiness?\n\n";
				cin >> type;
			}while(type!='P' && type!='B');
			contact[tempModify].pOrBType = (type =='P' ? contactParameter::personal : contactParameter::business); 
			break;
		default:
			cout << "You have entered an invalid choice, please enter a valid integer based on your choice.";
			updateContact();
			break;
 
	}		
 
}
 
void deleteContact()	
{
	string str;
	int numDelete;
	for(int i = 0; i < 250; i++)
	{
		if(contact[i].validData == true)
		{
			cout << i << " " << contact[i].name << "\n\n";
		}
	}
	cout << "Please enter the number of the contact you wish to delete.\n\n";
	getline(cin,str);
	numDelete = atoi (str.c_str());
	contact[numDelete].validData = false;
}
 
void searchContact()
{
	
	int temp;
	string str;
	
	while(temp!= 0 || temp!= 1)
	{
		cout << "Please enter a '0' if you are searching by name or a '1' if you are searching by e-mail address\n\n";
		getline (cin,str);
		temp = atoi (str.c_str());
	}
	if(temp == 0)
	{
		cout << "Please enter the name you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].name,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
			}
		}	
			
	}
	if(temp == 1)
	{
		cout << "Please enter the e-mail address you are looking for.\n\n";
		getline (cin,str);
		
		for(int i =0; i < 250; i++)
		{
			if(strcmp(contact[i].emailAddress,str.c_str()) == 0)
			{
				cout << "Name: " << contact[i].name << "\n\nemail adress: " << contact[i].emailAddress << "\n\ncell phone number: " <<contact[i].cellPhoneNumber
					 << "\n\nbirth date:  "<< contact[i].birthday.date << "\n\nbirth month: " << contact[i].birthday.month << "\n\nbirth year: " << contact[i].birthday.year
					 << "\n\ncontact type: " << contact[i].pOrBType;
			}
			else
			{
				cout<< "\n\nEntry not found or end of search\n\n";
			}
		}	
			
	}
		
}
 
void printContacts()
{
	int tempPrint;
	string str;
	bool swapped = true;
	
	do	{
			cout << "Please enter 0 if you would like to view personal contacts or 1 if you would like to view business contacts.\n\n";
//			getline(cin,str);
//			tempPrint = atoi (str.c_str());
			cin >> tempPrint;
	}while(tempPrint!= 0 && tempPrint!= 1);
 
	 
	if(tempPrint == 0)
	{
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 0)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << "Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
	if(tempPrint == 1)
	{
		cout << "Business!";
		swapped = true;
		do
		{
			swapped = false;
			for(int i = 0; i < 250; i++)
			{
				if( strcmp(contact[i].name,contact[i+1].name) < 0 && contact[i].pOrBType == 1)
				{
					swapped = true;
					contact[i] = tempContactHolder[i];
					contact[i] = contact[i+1];
					contact[i+1] = tempContactHolder[i]; 
					
				}
			}
		}
		while(swapped);
		
		for(int i = 0; i <250; i++)
		{
			if( contact[i].validData == true)
			{
				cout << i << " Name: " << contact[i].name << "\n\nCell Phone Number: " << contact[i].cellPhoneNumber << "\n\nemail address: " << contact[i].emailAddress;
				if(contact[i].birthday.date != -1)
				{
					cout<< "\n\nBirthday: " << contact[i].birthday.date << " " << contact[i].birthday.month << " " << contact[i].birthday.year;
				}
			}
		}
	}
 
}

Open in new window

0
 
Jaime OlivaresCommented:
>> I have never written an array of structs to a file before, anyone have a resource that tells a way, or is writing structs to a binary file the same as any other data type?
I think that deserves another question
0
 
pacman32689Author Commented:
I agree, I will open new question :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 12
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now