Link to home
Start Free TrialLog in
Avatar of zoe_in_limbo
zoe_in_limbo

asked on

Help with Address Book Project -- arrays, structures, etc

Hi, I have a project that I am working on, it is extremely important and due in a little over 24 hours. I have a good start on it, but I know I have a lot of work to do. Can you help me through some of the next steps?

I am a beginner and I am not asking for answers, just hoping that I can get some help while I work on this project.

Here is the problem::

--------------


you are going to construct functionality to create a simple address book. Conceptually the address book uses a structure to hold information about a person and an array of structures to hold multiple persons (people).

Visually think of the address book like so:

http://img527.imageshack.us/img527/6162/address114630941476389yr2.jpg



When you add a person to the Address Book you add a structure with the information about the person to the end of the array:

http://img296.imageshack.us/img296/9633/address215491261552405dw1.jpg

When you get a person, you get the first person in the address book. With each successive call to get a person, you get the next person in the array. For instance the first call to get a person you will get "Joe Smith" when you make the second call to get a person you would get "Jane Doe" so on and so forth. After you get the last person from the array the next call to get a person will start over at the beginning ("Joe Smith" in this case).

 

 

Details:
1.) Create a project called addressBook
2.) Add a header file and cpp file for your project
3.) All of your definitions should go in the header file.
4.) In the header file create the definition for your structure. Call it PERSON.
5.) Your structure should have fields for first name, last name, address, and optionally a phone number.
6.) Inside the cpp file you will create the functionality for your address book
7.) Inside the cpp file declare a global array of 10 PERSONS to hold all of the records in your address book call it people. Use a const called MAXPEOPLE to set the size of the array. Put the const in the header file.
8.) You are probably going to want to declare an integer variable to keep track of where you are at in the array.
9.) Create functions addPerson, getPerson
10.) These functions should take as arguments a reference to a PERSON structure.
11.) The addPerson method should copy the  structure passed to it to the end of the array
12.) the getPerson should start at array element 0 and with each successive call return the next person in the array.
13.) Create overloaded findPerson functions. One function should take only the persons last name
14.) The other function should take both the persons last and first names.
15.) All code for the functions should be in the cpp file
16.) From main write functionality that will test your address book code

 All of the functions that are part of the address book should take a reference to a PERSON structure as one of its arguments. This is not necessarily the only argument for each function but should be one of them.

------------------



As you can see, he really got some steps laid out, but most all of the logic is left unsaid and I seem to have a grasp on the code and understand the layout and what everything needs to do, but I am kind of at a stand still because I am getting errors not related to the obvious syntax errors like having // before some stuff causing syntax but I think there are some errors with the passing of arguments. I would like to make sure that I am passing things correctly and eliminate all the errors as I can before moving on.

Please help me out a bit and let me know if there is something I am really not getting right. Please let me know if I am going off track with something. I am a beginner and I cannot use anything but my header and iostream, ctime, cmath, etc.



HEADER FILE
 
 
const int MaxPeople = 10;
 
struct person
	{
		char first;
		char last;
		char *address;
		char *number;
	};
 
int i = 0;
 
 
HERE IS THE MAIN CPP FILE
 
 
#include <iostream>
#include "header.h"
 
using namespace std;
void addPerson(char bookArray[MaxPeople]);
void getPerson(char thisPerson);
int findPerson(char last, char thisPerson);
int findPerson(char thisPerson);
 
 
 
char bookArray[MaxPeople];	
int main()
{
	person pers;
	int x = 1;
	while (x == 1);
	{
		char choice;
		cout << "Address Book" << endl<< endl;
		cout << " Press 1 to add to the address book. " <<endl;
		cout << " Press 2 to get a person. " << endl;
		cout << " Press 3 to find a person" << endl;
		cout << " Press 4 to find a person by last name and first " << endl;
		cout << " Press 5 to find a person by last name only " << endl;
		cout << " Press 6 to quit " << endl;
		cout << endl << "Please make a selection:  ";
		cin >> choice;
		switch (choice)
		{
			case '1':
				addPerson(pers);
				
				break;
			case '2':
				getPerson(pers);
 
				break;
			case '3':
				findPerson(pers.last,pers);
				
				break;
			case '4':
				findPerson(pers);
				break;
			case '5':
				findPerson( pers.last,pers);
				break;
			case '6':
			default:
				cout << "Please make a valid selection: "
				cin >> choice;
				break;
 
 
		}
 
	i++;
	}
return 0;
}
 
 
void addPerson(char addArray[MaxPeople])
	{
		
		person add;
		cout << "Enter First Name" << endl;
		cin >> add.first;
		cout << "Enter Last Name" << endl;
		cin >> add.last;
		cout << "Enter Address" << endl;
		cin.getline(add.address, 40);
		cout << "Enter Address" << endl;
		cin.getline(add.number, 12);
		addArray[i] = add;
		
	}
 
void getPerson(char thisPerson)
	{
		// Gets a person in array[0] then next time it is called it will get array[1] and so forth. When it reaches the end of the array it will return the original array[0]
	}
 
int findPerson (char last, char thisPerson)
	{
	return (//the search result);
	}
 
int findPerson (char thisPerson)
	{
	return (//the search result);
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zoe_in_limbo
zoe_in_limbo

ASKER

Thanks infinity, I have responded to your comments and have made changes.


>> int i = 0;

This should not be in your header file. It's better placed in the .cpp file.

-- Ok, I changed the name to maincounter and I have moved it to the .cpp file.


>> 10.) These functions should take as arguments a reference to a PERSON structure.

A function that takes a reference to a type looks like this for example :

        void fun(int &ref) {
            ref = 5;
        }

Function 'fun' takes as argument a reference to an int. This means that when the function is called like this :

        int test = 1;
        fun(test);                     // <--- passed by reference

'test' will have the value 5 after calling the 'fun' function. The function updates the original int directly.

This works similarly for structs ... Give it a try.

Here's more info on references :

        http://www.cplusplus.com/doc/tutorial/functions2.html


-- So, I should try this...?
-- getPerson( &pers);

-- Now that I think about it, I think I would need to pass my entire array bookArray to the function, because it will need to extract the data from a certain array section. I think having a new counter variable that will progress when this gets accessed since it is supposed to display the next piece of data in the array.

So it would read:

int getcount = 0;   // this is the counter for the getPerson function. It would be global.
...
getPerson( &bookArray);   // call the function
....
getPerson( char *arr[MaxPeople])
{
cout << arr[getcount] << endl;
getcount++;
}


Am I on the right track here?? Sorry for being so slow with this but I am just learning.





>> 11.) The addPerson method should copy the  structure passed to it to the end of the array

This means that the person struct passed by reference should be added at the end of the array.
Ie., you shouldn't do any I/O inside the addPerson function.

-- Now here should I put the I/O inside of the switch/case for the add case?

Code looking something like this...

switch (choice)
        {
            case '1':
                person add;
                cout << "Enter First Name" << endl;
                cin >> add.first;
                cout << "Enter Last Name" << endl;
                cin >> add.last;
                cout << "Enter Address" << endl;
                cin.getline(add.address, 40);
                cout << "Enter Address" << endl;
                cin.getline(add.number, 12);
                addPerson(add);                
                break;


void addPerson(char *addArray[MaxPeople])
    {
       
    bookArray[maincounter] = addArray[maincounter];
    maincounter++;    
    }



>> 12.) the getPerson should start at array element 0 and with each successive call return the next person in the array.

Implementation missing ;) Use the int i you defined earlier to keep track of where you are in the array.


-- I believe I addressed this earlier in my update comments by using a new counter that will keep track of a counter int every time this specific function is called, that way it will display them in order???

>> int findPerson(char thisPerson);

A char is a single character. That's not enough to store a last or first name. A char* is a pointer to one or more characters, generally seen as a string of characters, terminated by a '\0' character.
You might consider using the std::string instead though ...


-- Wow, yea I just noticed that I did that. So I would have to declare it as:
(char * thisPerson[MaxPeople])
??





Thanks again for your help, I will post up updated code as it comes along....

Another question:



            case '1':
                person add;
                cout << "Enter First Name" << endl;
                cin >> add.first;
                cout << "Enter Last Name" << endl;
                cin >> add.last;
                cout << "Enter Address" << endl;
                cin.getline(add.address, 40);
                cout << "Enter Address" << endl;
                cin.getline(add.number, 12);
                addPerson(add);                
                break;


// The addPerson function gets called....

void addPerson(char *addArray[MaxPeople])
    {
       
    bookArray[maincounter] = addArray[maincounter];
    maincounter++;    
    }


Here I am trying to pass the add structure with the just inputted values to the addPerson function. If I sent a structure to it with the values and I need to function to write those values into the main array, then how would I pass it. It says I cannot convert to a char, but I honestly do not know what else to do?
>> -- So, I should try this...?
>> -- getPerson( &pers);

You need to specify the type too. In my example, the type was int.


>> I think I would need to pass my entire array bookArray to the function,

No, the array is already accessible to the function, since it is globally defined in the .cpp file.


>> Am I on the right track here??

I would suggest reading the tutorial I posted on passing arguments by reference to a function. It's a short but interesting read. And I'm sure you'll understand it better after that.


>> -- Now here should I put the I/O inside of the switch/case for the add case?

Since your teacher didn't specify this, you can place it there, yes.

You'll still need to fix the function signature though :

>> void addPerson(char *addArray[MaxPeople])

See the earlier remarks on passing by reference.



>> -- Wow, yea I just noticed that I did that. So I would have to declare it as:
>> (char * thisPerson[MaxPeople])

No, that would make 'thisPerson' an array of MaxPeople char*'s (C strings).
You want to pass only one string.
>> It says I cannot convert to a char, but I honestly do not know what else to do?

This is because you're not correctly passing the person struct by reference. See my previous comments ...
Thanks for the article on passing by reference. I understand that when you pass by reference, you actually pass the memory location so that the receiving function can actually make changes to the passed item. That makes sense.

I hope this is on the right track? I really am learning this all as we go so thanks for bearing with me...



switch (choice)
		{
 
			case '1':
				
				cout << "Enter First Name" << endl;
				cin >> add.first;
				cout << "Enter Last Name" << endl;
				cin >> add.last;
				cout << "Enter Address" << endl;
				cin.getline(add.address, 40);
				cout << "Enter Address" << endl;
				cin.getline(add.number, 12);
				addPerson( &add);		//trying to send these values entered into the structure add to the function addPerson.		
				break;
 
 
void addPerson(struct &addArray) // Need to figure out this. How to declare the new structure that will be used when inputting the structure to the bookArray[maincounter]
	{
	bookArray[maincounter] = addArray;
	maincounter++;
	}

Open in new window

>> void addPerson(struct &addArray)

The type is incomplete. struct is a C++ keyword ... What kind of struct is it ?

Btw, you won't be passing an array as parameter - you will be passing a reference to one person instance. So, naming the parameter 'addArray' is misleading.
>> addPerson( &add);

Note that the & operator here takes the address of the 'add' object. You don't need that. Passing by reference does not require the address operator.
Okay I am off work and will be working on this all night. Again, cannot say how thankful I am that you are putting in the time to help me through this.




I think I figured it out. (well at least this part that we are discussing.


int main()
{
addPerson(add);
}


void addPerson(struct person &add)
{
}

Correct? It seems I am no longer getting errors with these lines.
Posting current whole code so we are all on the same page:







Here is the header:

const int MaxPeople = 10;

struct person
    {
        char *first;
        char *last;
        char *address;
        char *number;
    };

.cpp is pasted below:

#include <iostream>
#include "header.h"
 
int maincounter = 0;
int getcount = 0; 
using namespace std;
void addPerson(struct person &add);
void getPerson(struct person &get);
void findPerson(char last, char thisPerson);
void findPerson(char thisPerson);
 
 
char bookArray[MaxPeople];	//This is the main "address book" array
 
 
int main()
{
	person find;
	person add;
		char choice;
		cout << "Address Book" << endl<< endl;
		cout << " Press 1 to add to the address book. " <<endl;
		cout << " Press 2 to get a person. " << endl;
		cout << " Press 3 to find a person by last name only" << endl;
		cout << " Press 4 to find a person by last name and first " << endl;
		cout << " Press 5 to quit " << endl;
		cout << endl << "Please make a selection:  ";
		cin >> choice;
		switch (choice)
		{
 
			case '1':
				
				cout << "Enter First Name" << endl;
				cin >> add.first;
				cout << "Enter Last Name" << endl;
				cin >> add.last;
				cout << "Enter Address" << endl;
				cin.getline(add.address, 40);
				cout << "Enter Address" << endl;
				cin.getline(add.number, 12);
				addPerson(add);	
				break;
 
			case '2':
				cout << "Getting the next person in the address book: " << endl << endl;
				char *getperson = bookArray[getcount];
				getPerson(getperson);
				getcount++;
				break;
 
			case '3':
				char *last = new char[20]
				cout << "Please enter the last name to search for: " << endl;
				cin >> find.last;
				findPerson(find.last);
				
				break;
			case '4':
				cout << "Please enter the last name and then the first name to search for: " << endl;
				cout << "Last name: ";
				cin >> find.last;
				cout << "First name: ";
				cin >> find.first;
				findPerson(find.last, find.first);
				break;
			case '5':
				return 0;
				break;				
			default:
				cout << "Please make a valid selection: " << endl;
				cin >> choice;
				break;
	}
 
return 0;
}
 
 
void addPerson(struct person &add) 
	{
	bookArray[maincounter] = add;
	maincounter++;
	}
 
void getPerson(struct person &get)
	{
	get = bookArray[getcount]
	cout << get[getcount] << endl;
	
	}
void findPerson (char &last)
	{
		for (int i=0; i<=MaxPeople; i++)
		if (last == person.last)
		/* I have a feeling my logic is incorrect using a counter to check each array value for the last name. */ 
	}
void findPerson (char &last, char &first)
	{
		for (int i=0; i<=MaxPeople; i++)
		if (last == person.last || first == person.first)
		/* I have a feeling my logic is incorrect using a counter to check each array value for the last and first names. */ 
				
	}

Open in new window

So I believe that the getPerson function would need NO parameters associated with it?

Since it will simply use the bookArray and output the values for a sequential array location.

For example. It is called the 1st time. It will output the values in bookArray[0]. The next time it is called, it will output the values from bookArray[1].

Somewhat like this:

 case '2':
          cout << "Getting the next person in the address book: " << endl << endl;
          getPerson();
          getcount++;
          break;


void getPerson()
    {
    cout << bookArray[getcount] << endl;
    }



Yes? No? Maybe so?

 It seems that I am not getting errors with this type of setup, I just will not know if it is doing what I want it to do until I can get the entire project to compile.

New Code:

(header is the same)


#include <iostream>
#include "header.h"
 
int maincounter = 0;
int getcount = 0; 
using namespace std;
void addPerson(struct person &add);
void getPerson();
void findPerson (struct person &find);
void findPerson (struct person &findlast, struct person &findfirst);
 
 
char bookArray[MaxPeople];	//This is the main "address book" array
 
 
int main()
{
	person find;
	person add;
	cout << "Address Book" << endl<< endl;
	while (true)
	{
		char choice;
		
		cout << " Enter 1 to add to the address book. " <<endl;
		cout << " Enter 2 to get a person. " << endl;
		cout << " Enter 3 to find a person by last name only" << endl;
		cout << " Enter 4 to find a person by last name and first " << endl;
		cout << " Enter any other key to quit " << endl;
		cout << endl << "Please make a selection:  ";
		cin >> choice;
		switch(choice)
		{
 
			case '1':
				
				cout << "Enter First Name" << endl;
				cin >> add.first;
				cout << "Enter Last Name" << endl;
				cin >> add.last;
				cout << "Enter Address" << endl;
				cin.getline(add.address, 40);
				cout << "Enter Address" << endl;
				cin.getline(add.number, 12);
				addPerson(add);	
				break;
 
			case '2':
				cout << "Getting the next person in the address book: " << endl << endl;
				getPerson();
				getcount++;
				break;
 
			case '3':
				char *last = new char[20];
				cout << "Please enter the last name to search for: " << endl;
				cin >> find.last;
				findPerson(find.last);
				
				break;
			case '4':
				cout << "Please enter the last name and then the first name to search for: " << endl;
				cout << "Last name: ";
				cin >> find.last;
				cout << "First name: ";
				cin >> find.first;
				findPerson(person, find);
				break;				
			default:
				return 0;
				break;
		}
	}
return 0;
}
 
 
void addPerson(struct person &add) 
	{
	bookArray[maincounter] = add; //adds the structure passed to it to the location on the main array.
	maincounter++; // progresses to next location on main array.
	}
 
void getPerson()
	{
	cout << bookArray[getcount] << endl;
	// I remember learning to not use I/O inside of a function.
	}
 
 
void findPerson (struct person &find)
	{
		for (int i=0; i<=MaxPeople; i++)
		{
			if (find == person.last)
				{
				// Display all the data from the array location that holds the structure with the matching value.
				}
		}
		/* I have a feeling my logic is incorrect using a counter to check each array value for the last name. */ 
	}
 
void findPerson (struct person &findlast, struct person &findfirst)
	{
		for (int i=0; i<=MaxPeople; i++)
			{
				if (findlast == person.last || findfirst == person.first)
					{
					// Display all the data from the array location that holds the structure with the matching values.
					}
			}
 
 
			/* I have a feeling my logic is incorrect using a counter to check each array value for the last and first names. */ 
					
	}

Open in new window

void addPerson(struct person &add)  //passing the add structure created above.
    {
    bookArray[maincounter]=add; //adds the structure passed to it to the location on the main array. I GET ERROR HERE
    maincounter++; // progresses to next location on main array.
    }


bookArray[maincounter]=add;


//adds the structure passed to it to the location on the main array. I GET ERROR HERE
Please, anyone, I am really at a stand still with this problem.

My current code is below, with comments, and the header is above.

Right now I am really stumped on passing the correct arguments, and receiving them correctly to a new variable for each function. I believe that the addPerson function is working properly, however I cannot test it unless the whole application compiles. I am literally spending all of my time searching google for tips and syntax on passing structures and adding those structures to arrays but nothing is really helping. I need one on one support. If anyone can help me out, I would really appreciate it.

#include <iostream>
#include "header.h"
 
int maincounter = 0;
int getcount = 0; 
using namespace std;
void addPerson(struct person &add);
void getPerson();
void findPerson (struct person &find);
void findPerson (struct person &findlast, struct person &findfirst);
 
 
char bookArray[MaxPeople];	//This is the main "address book" array
 
 
int main()
{
	
	person add;
	cout << "Address Book" << endl<< endl;
	while (true)
	{
		char choice;		
		cout << " Enter 1 to add to the address book. " <<endl;
		cout << " Enter 2 to get a person. " << endl;
		cout << " Enter 3 to find a person by last name only" << endl;
		cout << " Enter 4 to find a person by last name and first " << endl;
		cout << " Enter any other key to quit " << endl;
		cout << endl << "Please make a selection:  ";
		cin >> choice;
 
		switch(choice)
		{
 
			case '1':
				
				cout << "Enter First Name" << endl;
				cin >> add.first;
				cout << "Enter Last Name" << endl;
				cin >> add.last;
				cout << "Enter Address" << endl;
				cin.getline(add.address, 40);
				cout << "Enter Address" << endl;
				cin.getline(add.number, 12);
				addPerson(add);	
				break;
 
			case '2':
				cout << "Getting the next person in the address book: " << endl << endl;
				getPerson();
				getcount++;
				break;
 
			
			case '3':
				char *last = new char[20];
				cout << "Please enter the last name to search for: " << endl;
				cin >> find.last;
				findPerson(find);
				
				break;
			
				case '4':
				cout << "Please enter the last name and then the first name to search for: " << endl;
				cout << "Last name: ";
				cin >> find.last;
				cout << "First name: ";
				cin >> find.first;
				findPerson(person, find);
				break;
 
			default:
				return 0;
				break;
		}
	}
return 0;
}
 
 
void addPerson(struct person &add) 
	{
	bookArray[maincounter]=add; //adds the structure passed to it to the location on the main array. I GET ERROR HERE
	maincounter++; // progresses to next location on main array.
	}
 
void getPerson()
	{
	cout << bookArray[getcount] << endl;
	// I remember being told to not use I/O in a function. How else could this work?
	}
 
 
void findPerson (struct person &find)
	{
		for (int i=0; i<=MaxPeople; i++)
		{
			if (find == person.last)
				{
				// Display all the data from the array location that holds the structure with the matching value.
				}
		}
		 //I have a feeling my logic is incorrect using a counter to check each array value for the last name.
	}
 
 
 
void findPerson (struct person &findlast, struct person &findfirst)
	{
		for (int i=0; i<=MaxPeople; i++)
			{
				if (findlast == person.last || findfirst == person.first)
					{
					// Display all the data from the array location that holds the structure with the matching values.
					}
			}
 
 
			//I have a feeling my logic is incorrect using a counter to check each array value for the last and first names.
					
	}

Open in new window

>> int main()
>> {
>> addPerson(add);
>> }
>> 
>> 
>> void addPerson(struct person &add)
>> {
>> }
>> 
>> Correct? It seems I am no longer getting errors with these lines.

That looks good, yes :) Now the function is taking as argument a reference to a person struct. And in main, you're calling the function by providing a person struct as argument. That's good :)


>> So I believe that the getPerson function would need NO parameters associated with it?

The assignment says that it has to. The nice thing about references as arguments is that they work in both directions. Not only can you pass data TO the function, but you can also pass data FROM the function to the calling code.
The idea of the getPerson is to get the next person in the array, and return it via the function argument.



>>     bookArray[maincounter]=add; //adds the structure passed to it to the location on the main array. I GET ERROR HERE

Take a look at how you defined your bookArray. It is an array of char's. It needs to be an array of person structs.


>> void findPerson (struct person &find);
>> void findPerson (struct person &findlast, struct person &findfirst);

The findPerson functions are supposed to look for a person in the address book, by providing a last name and/or a first name. That's what you should provide as argument(s) to these functions. How are first and last names represented ?



>> If anyone can help me out, I would really appreciate it.

I hope that's what I'm doing ;) I'm not available 100% of the time (I have to sleep, eat etc. too some time heh), but I will do my best to guide you.


>> So I believe that the getPerson function would need NO parameters associated with it?

The assignment says that it has to. The nice thing about references as arguments is that they work in both directions. Not only can you pass data TO the function, but you can also pass data FROM the function to the calling code.
The idea of the getPerson is to get the next person in the array, and return it via the function argument.

Man, thanks for reminding me that it is REQUIRED.
So even though the function is void, I can still return values? Interesting.
I would assume that I would NOT use a return(); statement though?

struct person new = getPerson( still not sure what to pass to it!! )

void getPerson( struct person &arg )
      {
      arg = bookArray[getcount];
      }
 

>>     bookArray[maincounter]=add; //adds the structure passed to it to the location on the main array. I GET ERROR HERE

Take a look at how you defined your bookArray. It is an array of char's. It needs to be an array of person structs.

That makes sense, so since my array is filled with nothing but structures, the array needs to be defined as an array of structures.....

so I can define the array as  person bookArray[MaxPeople]; ??
perhaps struct personbookArray[MaxPeople]???


>> void findPerson (struct person &find);
>> void findPerson (struct person &findlast, struct person &findfirst);

The findPerson functions are supposed to look for a person in the address book, by providing a last name and/or a first name. That's what you should provide as argument(s) to these functions. How are first and last names represented ?

I think they should be represented as character strings because each one is a single word. So I should make a new character string, one for last name, and one for first name and assign the user inputted data to that string? After that, pass that character string to the array, the array will then use a for loop to search through each structure in the main array to see if the character string = structurename.last and character string = structurename.firstAm I thinking this correctly?


>> If anyone can help me out, I would really appreciate it.

I hope that's what I'm doing ;) I'm not available 100% of the time (I have to sleep, eat etc. too some time heh), but I will do my best to guide you.

I know I know, I am just freaking out! This is hard stuff when it is completely foreign. Google is a great help, but I can only find general ideas, and many examples are using tactics that I am not allowed to use. There is only one way to learn it and that is by buckling down and doing it. Sadly, I have spent hours and hours on this seemingly SIMPLE assignment. I have done PC repair and troubleshooting professionally for years, but never got into coding, only had simple VB training, and that was it before this. It is not super hard, just takes hours of practice before memorizing it. I am sure this stuff just comes naturally to you.... Thanks again for all your help, it is like having a tutor! I am working hard and thank your for the time and effort
>> void getPerson( struct person &arg )
>>      {
>>      arg = bookArray[getcount];
>>      }

This is correct, except that you still need to increment getcount (do not forget to loop back to 0 when you reach the end of the array - you can use the modulo operator % for that ...)

And you'd call it just like you called addPerson. After calling the getPerson function, the person struct you passed as argument will contain the next person from the array.


>> That makes sense, so since my array is filled with nothing but structures, the array needs to be defined as an array of structures.....

Correct.

>> so I can define the array as  person bookArray[MaxPeople]; ??
>> perhaps struct personbookArray[MaxPeople]???

Both would work in C++. 'struct person' and 'person' are synonyms so to say.


>> I think they should be represented as character strings because each one is a single word.

You're thinking correctly ;)


>> So I should make a new character string, one for last name, and one for first name and assign the user inputted data to that string?

Indeed.


>> After that, pass that character string to the array, the array will then use a for
>> loop to search through each structure in the main array to see if the
>> character string = structurename.last and character string = structurename.first
>> Am I thinking this correctly?

That's it. But you'll have to implement this for loop yourself ...

Btw, as I said earlier, consider using an STL string instead of a C string (char*). They're easier to use :

        http://www.cplusplus.com/reference/string/string/



>> Sadly, I have spent hours and hours on this seemingly SIMPLE assignment.

Don't worry about that. Those hours are well spent, since you are learning new things while you're working on this. Just remember, that the next time, things like this will be a lot easier :)


>> I am sure this stuff just comes naturally to you...

It will to you too. It just takes practice :)
Is this syntax legal?



cout << getPerson(bookArray) << endl;

or would I have to assign the result of the function call to a variable.
such as:

person new = getPerson(bookArray);cout << new << endl;
person getStruct;

            case '2':
                cout << "Getting the next person in the address book: " << endl << endl;
                getStruct = getPerson(getStruct)
                cout << getStruct << endl;
                break;

//The error log is complaining about the '=' because the function is void
" binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)"



void getPerson(struct person &get)
    {
        if (getcount > maincounter)
            {
                getcount = 0;
                get = bookArray[getcount];
                getcount++;
            }
        else
            {
                get = bookArray[getcount];
                getcount++;
            }
    }


>> cout << getPerson(bookArray) << endl;

First of all : bookArray is your address book. It is an array of person structs. Is that what you want to pass as argument ?

Second : getPerson isn't returning anything, so you can't use the returned value (since there is none).

>> person new = getPerson(bookArray);

The same is true here. Note btw that 'new' is a C++ keyword, so you cannot use it as variable name.



>> //The error log is complaining about the '=' because the function is void

Same reason : the function is not returning anything, so you cannot use the return value (since there is none).
Did you read the tutorial I posted earlier on passing arguments to functions by reference ?

        http://www.cplusplus.com/doc/tutorial/functions2.html

Did you fully understand everything ? If not, do not hesitate to ask for clarification where needed.
Yes, I noticed that about passing the array, so I changed it.


Basically when I pass by reference, any changes that I make to the get variable above (in the getPerson function is going to make the changes to the getStruct. So back in main I can print getStruct and it will print the values for get?

Like This:



person getStruct;
            case '2':
                cout << "Getting the next person in the address book: " << endl << endl;
                getPerson(getStruct)
                cout << getStruct << endl;
                break;
 
 

void getPerson(struct person &get)
    {
        if (getcount > maincounter)
            {
                getcount = 0;
                get = bookArray[getcount];
                getcount++;
            }
        else
            {
                get = bookArray[getcount];
                getcount++;
            }
    }

?????
>> Basically when I pass by reference, any changes that I make to the get variable above (in the getPerson function is going to make the changes to the getStruct. So back in main I can print getStruct and it will print the values for get?

Correct.


>>                 getPerson(getStruct)
>>                cout << getStruct << endl;

Indeed. (assuming that 'getStruct' is a person struct)
Okay moving onto the search functions, now the more complex one is the one regarding sending the last name AND the first name as search terms, so I think that would be the best to search for.

But first I decided to comment out all things related to the search functions and tried to compile. I still am getting a massive error list and a failed compile.

Below is the code (With the search functions and things related to them commented out). The header is the same.

I have about 3 hours to make as much progress as I can!
I am going to start troubleshooting but hopefully you can help me out some more regarding these errors.


Here is my error list:


p.s. --u rule infinity


#include <iostream>
#include "header.h"
 
int maincounter = 0;
int getcount = 0; 
using namespace std;
void addPerson(struct person &add);
void getPerson(struct person &get);
//void findPerson (struct person &find);
//void findPerson (struct person &findlast, struct person &findfirst);
 
 
person bookArray[MaxPeople];
 
int main()
{
	char *last = new char[20];
	char *first = new char[20];
	char choice;
	person add;
	person getStruct;
	
	cout << "Address Book" << endl<< endl;
 
	while (true)
		{	
			cout << " Enter 1 to add to the address book. " <<endl;
			cout << " Enter 2 to get a person. " << endl;
			cout << " Enter 3 to find a person by last name only." << endl;
			cout << " Enter 4 to find a person by last name and first. " << endl;
			cout << " Enter any other key to quit. " << endl;
			cout << endl << "Please make a selection:  ";
			cin >> choice;
 
			switch(choice)
			{
 
				case '1':
					
					cout << "Enter First Name" << endl;
					cin >> add.first;
					cout << "Enter Last Name" << endl;
					cin >> add.last;
					cout << "Enter Address" << endl;
					cin.getline(add.address, 40);
					addPerson(add);	
					break;
 
				case '2':
					cout << "Getting the next person in the address book: " << endl << endl;
					getPerson(getStruct);
					cout << getStruct << endl;
					break;
 
		/*
				case '3':
					cout << "Please enter the last name to search for: " << endl;
					cin >> last;
					findPerson(last);
					
					break;
				
				case '4':
					cout << "Please enter the last name and then the first name to search for: " << endl;
					cout << "Last name: ";
					cin >> last;
					cout << "First name: ";
					cin >> first;
					findPerson(last, first);
					break;  
 
		*/
 
 
				default:
					return 0;
					break;
			}
		}
return 0;
}
 
 
void addPerson(struct person &add) 
	{
	bookArray[maincounter]=add;
	maincounter++; 
	}
 
void getPerson(struct person &get)
	{
		if (getcount > maincounter)
			{
				getcount = 0;
				get = bookArray[getcount];
				getcount++;
			}
		else
			{
				get = bookArray[getcount];
				getcount++;
			}
	}
 
 
 
/*
void findPerson (struct person &find)
	{
		for (int i=0; i<=MaxPeople; i++)
		{
			if (find == person.last)
				{
				// Display all the data from the array location that holds the structure with the matching value.
				}
		}
		 //I have a feeling my logic is incorrect using a counter to check each array value for the last name.
	}
 
 
 
void findPerson (struct person &findlast, struct person &findfirst)
	{
		for (int i=0; i<=MaxPeople; i++)
			{
				if (findlast == person.last || findfirst == person.first)
					{
					// Display all the data from the array location that holds the structure with the matching values.
					}
			}
 
 
			//I have a feeling my logic is incorrect using a counter to check each array value for the last and first names.
					
	}
 
 
*/

Open in new window

I forgot the error lists:



1>------ Build started: Project: lab19, Configuration: Debug Win32 ------
1>Compiling...
1>lab19.cpp
1>c:\users\edork\documents\visual studio 2008\projects\lab19\lab19\lab19.cpp(52) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'person' (or there is no acceptable conversion)
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(700): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(738): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(785): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(909): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(916): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(923): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(930): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(170): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(176): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(183): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(190): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(210): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(243): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(263): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 int)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(288): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(308): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(328): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(349): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(369): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(390): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(410): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(430): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(450): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(470): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        while trying to match the argument list '(std::ostream, person)'
1>Build log was saved at "file://c:\Users\Edork\Documents\Visual Studio 2008\Projects\lab19\lab19\Debug\BuildLog.htm"
1>lab19 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Open in new window

>> But first I decided to comment out all things related to the search functions and tried to compile. I still am getting a massive error list and a failed compile.

It's indeed a good idea to first test what you already have before continuing. Just to make sure that everything you already did works.

I really suggest always doing that. Developing an application is usually best done in small increments : ie. add some functionality to it, then test that new functionality, next add some more functionality, and test that, etc.


>> I am going to start troubleshooting but hopefully you can help me out some more regarding these errors.

Feel free to post the errors you don't understand and/or don't know how to fix.

>> no operator found which takes a right-hand operand of type 'person'

This first error is pretty straightforward. There is no operator<< defined for the person struct.
If you haven't learned about operator overloading yet, then don't bother.

Instead, create a print function for the person struct, pass it a reference to a person struct, and make it print all its data members ... Call that function whenever you want to show a person to the user.
So...

// Pass a person struct as an argument

void printStruct(struct person &print)
{
cout << print.first << endl;
cout << print.last << endl;
cout << print.address << endl;
}

??

Sorry for the redundant questions, but it is just a waste of time to write stuff that does not work.
Whoa it succeeded, but the console crashed when I tried to add.
Working on this right now:

case '4':
					cout << "Please enter the last name and then the first name to search for: " << endl;
					cout << "Last name: ";
					cin >> last;
					cout << "First name: ";
					cin >> first;
					findPerson(findStruct2, last, first);
					break;  
 
 
 
 
void findPerson (struct person &findlast, struct person &findfirst)
	{
		for (int i=0; i<=MaxPeople; i++)
			{
				if (findlast == person.last || findfirst == person.first)
					{
					// If the last name passed and the first name passed = the structname.last and structname.first then send that structname to the print function.
					}
				else
				{
				cout << "Sorry, that entry was not found " << endl;
				}
			}
			//I have a feeling my logic is incorrect using a counter to check each array value for the last and first names.			
	}

Open in new window

Sorry, the above code is wrong:

Ignore that (I wish we could edit our posts!)

Here:



case '4':
	cout << "Please enter the last name and then the first name to search for: " << endl;
	cout << "Last name: ";
	cin >> last;
	cout << "First name: ";
	cin >> first;
	findPerson(findStruct2, last, first);
	break; 
 
 
 
 
 
void findPerson (struct person &findlast, char &last, char &first)
	{
		for (int i=0; i<=MaxPeople; i++)
			{
				if (findlast.last == last || findfirst.first == first)
					{
					// If the last name passed and the first name passed = the structname.last and structname.first then send that structname to the print function.
					}
				else
				{
				cout << "Sorry, that entry was not found " << endl;
				}
			}
			//I have a feeling my logic is incorrect using a counter to check each array value for the last and first names.			
	}

Open in new window

>> So...

Yes, that looks good :)


>> Whoa it succeeded, but the console crashed when I tried to add.

Can you show the entire code you're using now ? Are you correctly handling the char*'s ?

Did you consider using STL strings instead like I suggested earlier ?
Yea I know you suggested the STL strings, I just dont feel I have the time to learn a whole new concept.



lab19.txt
p.s. -- The code I just sent you has some sections commented out so that it will compile!
>> I just dont feel I have the time to learn a whole new concept.

It's your choice ... But they are a whole lot easier than C strings (char*). They are the preferred strings in C++ ... So, you will be confronted with them one day or another (and probably immediately like them ;) )



>>                               cin >> add.first;

You haven't allocated any memory for the 'first' member of the 'add' struct. It's currently just a char* pointing to some location in memory, which is most likely invalid.

You wouldn't have this problem with STL strings ;) You can use those just like you are doing now with the char*'s.
I will be sure to study up on STL strings. For now I need to keep it like this.

>.<

I have tried
char *first = new char[20];
char *first[20];
char first[20]; // This worked but this would not do what I need it to?

now using char first[20] worked and allowed the input, but it activated the while loop and just went back to the menu after I entered the LAST name.

I am getting stumped thinking of the logic to FIND a person

void findPerson (struct person &findlast, char &last, char &first)
    {

  for (int i=0; i<=MaxPeople; i++)

//somehow it needs to go to EACH section of the array, bookArray[i], and compare the value of findlast.first and findlast.first.
// I do not know how to look INSIDE bookArray[i] and check a specific value of the structure stored inside.



        for (int i=0; i<=MaxPeople; i++)
            {
                if (findlast.last == last || findfirst.first == first)
                    {
                    printStruct(findlast)
                    }
                else
                    {
                        cout << "Sorry, that entry was not found " << endl;
                    }
            }        
    }
Ok ok really really quick as I am running out of time...

With the search functions commented out, I can get the program to run and work properly BUT the address input is not working correctly. It just breaks outof the input and goes back to the menu.

The GET function works perfectly. So before I turn this in, I would like the address input to work correctly. I think it has to do with how the address string is inputted.
Great help man. I just ran out of time.
Can you show the complete code you are using now ?
Infinity, my teacher allowed us to wait until midnight tonight (united States Pacific Time) to turn in the assignment. I hope it is still a decent hour where you are so you can maybe help me troubleshoot a bit more. Here is the complete code as you requested:

(keeping in mind some is commented out)



Header...


const int MaxPeople = 10;

struct person
    {
        char first[20];
        char last[20];
        char address[50];
    };



#include <iostream>
#include "header.h"
 
int maincounter = 0;
int getcount = 0; 
using namespace std;
void addPerson(struct person &add);
void getPerson(struct person &get);
void printStruct(struct person &print);
//void findPerson (struct person &find);
//void findPerson (struct person &findlast, char &last, char &first);
 
 
person bookArray[MaxPeople];
 
int main()
{
	char *last = new char[20];
	char *first = new char[20];
	char choice;
	person add;
	person getStruct;
	person findStruct1;
	person findStruct2;
	
	cout << "Address Book" << endl<< endl;
 
	while (true)
		{	
			cout << endl << " Enter 1 to add to the address book. " <<endl;
			cout << " Enter 2 to get a person. " << endl;
			cout << " Enter 3 to find a person by last name only." << endl;
			cout << " Enter 4 to find a person by last name and first. " << endl;
			cout << " Enter any other key to quit. " << endl;
			cout << endl << "Please make a selection:  ";
			cin >> choice;
 
			switch(choice)
			{
 
				case '1':
					
					cout << "Enter First Name" << endl;
					cin >> add.first;
					cout << "Enter Last Name" << endl;
					cin >> add.last;
					cout << "Enter Address" << endl;
					cin.getline(add.address, 40); // This does not work, I was unsuccesful troubleshooting. it works if formatted like a standard cin >> add.address;
					addPerson(add);	
					break;
 
				case '2':
					cout << endl << "Getting the next person in the address book: " << endl << endl;
					getPerson(getStruct);
					printStruct(getStruct);
					break;
/*
		
				case '3':
					cout << "Please enter the last name to search for: " << endl;
					cin >> last;
					findPerson(findStruct1, last);
					
					break;
				
				case '4':
					cout << "Please enter the last name and then the first name to search for: " << endl;
					cout << "Last name: ";
					cin >> last;
					cout << "First name: ";
					cin >> first;
					findPerson(findStruct2, last, first);
					break;  
*/
 
				default:
					return 0;
					break;
			}
		}
return 0;
}
 
 
 
 
 
 
void addPerson(struct person &add) 
	{
		bookArray[maincounter]=add;
		maincounter++; 
	}
 
 
 
 
 
void getPerson(struct person &get)
	{
		if (getcount > maincounter)
			{
				getcount = 0;
				get = bookArray[getcount];
				getcount++;
			}
		else
			{
				get = bookArray[getcount];
				getcount++;
			}
	}
 
 
/*
 
 
void findPerson (struct person &find, char &last)
	{
		for (int i=0; i<=MaxPeople; i++)
		{
			if (find == find.first)
				{
					//If the last name passed = the structname.last then send that structname to the print function.
				}
			else
				{
					cout << "Sorry, that entry was not found " << endl;
				}
		}
		 //I have a feeling my logic is incorrect using a counter to check each array value for the last name.
	}
 
 
 
 
 
void findPerson (struct person &findlast, char &last, char &first)
	{
		for (int i=0; i<=MaxPeople; i++)
			{
				if (findlast.last == last || findfirst.first == first)
					{
					printStruct(findlast)
					}
				else
					{
						cout << "Sorry, that entry was not found " << endl;
					}
			}
			//I have a feeling my logic is incorrect using a counter to check each array value for the last and first names.			
	}
 
*/
 
 
void printStruct(struct person &print)
{
cout << print.first << " " << print.last << endl;
cout << print.address << endl;
}

Open in new window

>>                               cin >> add.first;

When you do this, you try to read a string into the 'first' member of the 'add' person struct. That's ok, but there's one problem : you didn't allocate memory for add.first - it is currently still pointing nowhere (or to a random, possibly invalid memory location).

You need to allocate enough memory for it first before reading in a value.

The same is true for the others of course.


Note also that, if you allocate memory, you need to de-allocate it some time later (when you don't need the memory any more), or you have a memory leak.


>>                               cin.getline(add.address, 40); // This does not work, I was unsuccesful troubleshooting. it works if formatted like a standard cin >> add.address;

This is for the same reason. No memory was allocated for add.address ...



>>             if (getcount > maincounter)

maincounter always points one element AFTER the last element. So, you need to modify this if statement slightly to correctly check for the last position.