# Structures and Arrays

Page 1 of 1

## 7 Replies - 3319 Views - Last Post: 07 October 2008 - 05:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=66528&amp;s=8f695ec0a544deab7c17d1819d282c2c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 killakev

Reputation: 0
• Posts: 22
• Joined: 01-September 08

# Structures and Arrays

Posted 06 October 2008 - 08:29 PM

Ok I have an assignment due.. and I need help tackling this assignment! Here is the info I have.. Im just really confused on how to start this project...

For these two labs 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:

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:

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

Is This A Good Question/Topic? 0

## Replies To: Structures and Arrays

### #2 KYA

• g++ jameson.cpp -o beverage

Reputation: 3153
• Posts: 19,194
• Joined: 14-September 07

## Re: Structures and Arrays

Posted 06 October 2008 - 08:35 PM

The site's policy is to show some effort before you're helped, but I'll give you an idea of how to proceed.

{
//What exactly is an address book?
//it is a listing of people's names, phone #
//So we're looking at a three dimensional array, yuck right?

//STL Maps would be ideal here since you can associate second
// tier information with the person's name as the key of the Map

//*******ALTERNATE IDEA*******
//Define a struct or another class called Person, it will
//contain person's name, address, etc... see below
}

struct Person //you can make an array of these
{
char* name; // or string, depends on what you feel like
int phoneNum;
string address; // or a char pointer/array, again up to you
}

Hope that helps!

### #3 no2pencil

• Professor Snuggly Pants

Reputation: 5892
• Posts: 28,868
• Joined: 10-May 07

## Re: Structures and Arrays

Posted 06 October 2008 - 08:36 PM

killakev, on 6 Oct, 2008 - 11:29 PM, said:

Ok I have an assignment due.. and I need help tackling this assignment! Here is the info I have.. Im just really confused on how to start this project...

Do you know how to make a struct?

I would suggest that you start there & if you need help with making one, I used one in my store code snippet.

### #4 killakev

Reputation: 0
• Posts: 22
• Joined: 01-September 08

## Re: Structures and Arrays

Posted 07 October 2008 - 10:38 AM

#include <iostream>
using namespace std;
bool getPerson (PERSON &p);
bool findPerson (char *frame, PERSON &p);
bool findPerson (char *frame, char *lName, PERSON &p);
void printBook();

struct PERSON
{
char fName[100];
char lName[100];
}

const int MAXPEOPLE 3
PERSON people[MAXPEOPLE];
int tail = 0;

{
{
return false;
}
return true;
}

bool getPerson(PERSON &p)
{
return false;
if(tail) = MAXPEOPLE)
{
tail = 0;
}
P= people[tail];
tail ++;
return true;
}

bool findPerson(char *lName, PERSON &p)
{
{
if(!stricmp(lName, people[i].lName)
{
p = people[];
return true;
}
}
return false;
}
void printBook()
{
{
cout<<people[i].fName<<endl;
cout<<people[i].lName<<endl;
}
}

int main()
{
PERSON peeps[]= {{"Joe", "Blow"}, {"Sam", "Smith"}, {"Bill","Jones"}};
PERSON p;
bool status= getPerson(p);
if (status == true)
cout<<p.lName<<" "<<p.fName;
else;
}

ok heres what I have done so far.. I tried the code and its not working can you go over for it me thnx

Reputation: 3
• Posts: 107
• Joined: 03-October 08

## Re: Structures and Arrays

Posted 07 October 2008 - 11:55 AM

i dunno what the first poster is talking about 3d arrays for, it seems the assignment wants a single dimension array of structures/objects.. this is actually a pretty easy assignment... you are doing this in c++ right ? i think they wanted seperate files for .h and implementation and driver file..

This post has been edited by Sadaiy: 07 October 2008 - 11:57 AM

### #6 KYA

• g++ jameson.cpp -o beverage

Reputation: 3153
• Posts: 19,194
• Joined: 14-September 07

## Re: Structures and Arrays

Posted 07 October 2008 - 01:35 PM

I mentioned 3D arrays for a reason. Let's say the assignment does not specify what type of data structure to use. Let's say the OP only had dealt with arrays of primitives. He or she may decide that the best way to go is with a 3D array to store information. It is an example of the planning process that helps produce better code.

### #7 killakev

Reputation: 0
• Posts: 22
• Joined: 01-September 08

## Re: Structures and Arrays

Posted 07 October 2008 - 05:50 PM

Sadaiy, on 7 Oct, 2008 - 11:55 AM, said:

i dunno what the first poster is talking about 3d arrays for, it seems the assignment wants a single dimension array of structures/objects.. this is actually a pretty easy assignment... you are doing this in c++ right ? i think they wanted seperate files for .h and implementation and driver file..

It only requires a header file... Yes i am doing this for c++ and I would like some input on my code and why it returns so many errors

Reputation: 3
• Posts: 107
• Joined: 03-October 08

## Re: Structures and Arrays

Posted 07 October 2008 - 05:56 PM

Line 2 says it wants header and a CPP file... you can't put int main() in a header file it must be in the cpp, so put your code for your header also so i can see all the code... you didn't put up your class/ head file code.. also i think you should put the struct in the header..