Hello, I am a new programmer attempting to tackle this puzzle after a couple of setbacks. I was excited about this project, but I am very stuck, and I am not sure where to go, or even if I'm on the right track. Okay, so let's get down to the problem: I am trying to write an address book; it needs to have ten people in it but right now I have everything set for two just for the sake of testing everything. (It takes ages to write in ten sets of addresses!) Anyway, what it needs to do is take the information of the people from the user and put it into an array of structures. What I am working on right now is the addPerson and getPerson functions: I'm not even going to worry about the others untill I can understand them better.

The addPerson function should copy the structure passed to it to the end of the array.
The getPerson should start at array element 0 and with each successive call return the next person in the array.

Both of these functions need to take a reference from a structure called PERSON, which is where all of the fields for the first and last names, and the address should be.

//Structure definition Person.
//contains definitions for Functionality.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;


struct PERSON
{	
	char firstName[20];
	char lastName[20];
	char address[50];
		
};
const int MAXPEOPLE = 2;
PERSON people[MAXPEOPLE];
PERSON p;

Now, this section is the functionality of the program. (this is my first time using a header file)

//.cpp file
//Contains functionality of addressBook

#include <iostream>
using namespace std;
#include "definitionsAddressBook.h"

int addPerson(PERSON p);
int getPerson(PERSON p);


int addPerson(PERSON p)
{ 
	for(int i = 0; i < MAXPEOPLE; i++)
	{
		PERSON people[MAXPEOPLE];
		
		people[0] = p;

	}
	
	return 0;
	
}
int getPerson(PERSON p)
{
	
	for(int i = 0; i < MAXPEOPLE; i++)
	{
		people[i];
	}

	return 0;
}

int main()
{ 
	cout << "This is an address book that can hold ten people. " << endl;
	
	for(int i = 0; i < MAXPEOPLE; i++)
	{
	cout << "Please enter the first name: " << endl;
	cin >> p.firstName;
	cout << "Please enter the last name: " << endl;
	cin >> p.lastName;
	cout << "Now enter the address: " << endl;
	cin >> p.address;
	addPerson(p);
	}
	getPerson(p);
	cout << people << endl;

}

I know that it is very rough, I am having a lot of trouble with my memory right now and that doesn't mix with learning new things. I would really appreciate some help, but I also want to say that this is my homework so please do not post a full answer: I really, really want to learn! I just need help with some explanations about structs and maybe how to use them in a situation like this. I'm not sure if I am referencing it properly in the functions with PERSON p. I also am trying very quickly to re-learn about arrays and how to manipulate them. If you can point me in the right direction, I would greatly appreciate it. Thank you very much, in advance :)

You're off to a good start, here are some little tips/observations that might help you:

1/ Your header file should contain the structure definition as well as the function prototypes for functions related to that structure. So basically you should move the function prototypes into your header. Remove the last line of the header, you do not need/want it there.

2/ You will need some variable that keeps track of how many people are currently in the address book. You need this so that you know where in the array to add a person.

3/ In you addPerson(), you do not need to loop, and you should not redeclare the array. Instead you should copy the contents of the given person to the index of the array that you are currently on (as mentioned in #2).

4/ getPerson() should not loop either. You should have a variable somewhere that keeps track of what index you have looked at last so that the next time you call this function, you can get the next person after that (and increase the index).

Please see my comments in the code.

//Structure definition Person.
//contains definitions for Functionality.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;


struct PERSON
{	
	char firstName[20];
	char lastName[20];
	char address[50];
		
};
const int MAXPEOPLE = 2;
PERSON people[MAXPEOPLE];
PERSON p; // you dont need this global variable
//.cpp file
//Contains functionality of addressBook

#include <iostream>
using namespace std;
#include "definitionsAddressBook.h"

int addPerson(PERSON p);
int getPerson(PERSON p);


int addPerson(PERSON p)
{ 
	for(int i = 0; i < MAXPEOPLE; i++)
	{
                /* this is a variable local to this loop. every time this function gets called a new array is created and you then add the element to the 0th location of this array. Do you need this or the global array declared in .h? */	

		PERSON people[MAXPEOPLE]; 	
		people[0] = p;

	}
	
	return 0;
	
}
int getPerson(PERSON p)
{
/* are you using 'p'( function argument) anywhere? if no then why add it to the function signature? */

	for(int i = 0; i < MAXPEOPLE; i++)
	{
		people[i];                
	}

	return 0;
}

int main()
{ 
	cout << "This is an address book that can hold ten people. " << endl;
	
	for(int i = 0; i < MAXPEOPLE; i++)
	{
       /* you can declare a variable of type person here and use that, everytime a new struct will be created which will passed by value to the addPerson function. */

	cout << "Please enter the first name: " << endl;
	cin >> p.firstName;
	cout << "Please enter the last name: " << endl;
	cin >> p.lastName;
	cout << "Now enter the address: " << endl;
	cin >> p.address;
	addPerson(p);
	}

        /*are you using the parameter anywhere in the function?*/
	getPerson(p);

       /*probably you can think of writing this cout in someother function, it should print not just one person but the entire array one -by-one*/
	cout << people << endl;

}

Thank you very much for taking the time to help me. I am going to play around with this and I'll reply with some of the questions that I will have then.

Thank you very much for your advice. This was helpful, and I appreciate it.

All right, well, this is almost done. I think. I believe I need some help with my switch structure. I think I am done with the logic of most of the functions. If someone can give me some advice or a push in the right direction, I would really appreciate it. I'm not going to post the function code in this thread because I know this website's policy about free homework, and this project is my homework. i don't want to give someone else the homework for this class. So here is my main function, perhaps someone can see what is wrong with the switch :)

int main()
{
	
	PERSON p;
	int mySwitch;
	char *lName = "ppppppppppppppppppppppppppppppppp";//I know it looks weird, I'm sorry. I had a warning and so I did this
	char *fName = "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL";//to initialize...
	char access;
	
	cout << "This is an address book that can hold ten people. " << endl;
	cout << "Would you like to access the address book? y/n" << endl;
	cin >> access;
	if(access == 'y')
	{
	cout << "To enter a contact into the address book, press 1. After you are finished, press 2." << endl;
	cout << "To view the entire address book, press 3: " << endl;
	cout << "To search the address book by last name, press 4: " << endl;
	cout << "To search the address book by last and first name, press 5: " << endl;
	cout << "To exit, press any other number. " << endl;
	cin >> mySwitch;
	while(mySwitch < 6)
	{

	switch(mySwitch)
		{
		case 1:
			
			cout << "Enter the first name: " << endl;
			cin.getline(p.firstName, 20);
			cout << "Enter the last name: " << endl;
			cin.getline(p.lastName, 20);
			cout << "Enter the address: " << endl;
			cin.getline(p.address, 50);

			addPerson(p);
cout << "To enter a contact into the address book, press 1. After you are finished, press 2." << endl;
	cout << "To view the entire address book, press 3: " << endl;
	cout << "To search the address book by last name, press 4: " << endl;
	cout << "To search the address book by last and first name, press 5: " << endl;
	cout << "To exit, press any other number. " << endl;
	cin >> mySwitch;

			break;

		case 2:

			cout << "To view the entire address book, press 3: " << endl;
			cout << "To search the address book by last name, press 4: " << endl;
			cout << "To search the address book by last and first name, press 5: " << endl;
			cout << "To exit, press any other button. " << endl;
			cin >> mySwitch;
			
			break;

		case 3:

			cout << getPerson;
			break;

		case 4:
					
		cout << "Please enter the last name of the person you would like to view: " << endl;
		cin >> lName;
		cout << findPerson(lName, p);

		break;

		case 5:

			cout << "Please enter the last and first names of the person you are searching for. " << endl;
			cin >> lName >> fName;
			cout << findPerson(lName, fName, p);
			break;

		default:
			cout << "Thank you for using the address book." << endl;
			exit(0);
			break;
		}
	}
	}
	else
		exit(1);
			
		return 0;

}

Alright, well, when I run this program, I get five warnings because of stricmp, but I'm not worried about that. What I need to know, is why my switch isn't working. If you need more info to help me figure it out, I will be playing around with it too, so I might be able to tell you more about it in a little while...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.