Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When you add a person to the Address Book you add a structure with the informati

ID: 3568734 • Letter: W

Question

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:

This lab is intended to prepare you to start working with object oriented programming. This means defining classes and creating objects. A class is an encapsulation of data and functions that operate on that data. An object is an instance of a class. Since we haven't covered how to create a class we are going to simulate it. How do we do that? Well hopefully it will be simple.

First off we need to determine what the data section is. In this case it is going to be the array that holds the structs (people). Because the functions you write need to operate on the data in some way you are going to have to put this array in globalscope. This way all of the functions have direct access to array without it having to be passed around. One thing of note and that is if we think about this from main's perspective main should not know how the data is stored or what it is stored in. All main should know is that if you use the addPerson function you can add a person to the address book. This is what we call a public interface to private data. Since we are putting the array in global scope in addressBook.cpp we pseudo encapsulate it. This means that only addressBook functions have access to the array and variable in global scope. Of course since it is in global scope there is a way around this but again, we are simulating.

When you add a person struct to the end of the array you are going to need to keep track of where you put it. This way you have an idea of where the end of the array is so that you can add the next person. You are also going to need to do this for the beginning of the array so that you know where to get the next person from. This means that along with the array that you have in global scope you are also going to have to create a couple of int variables to be used as indexes to keep track of where you are in the array. A suggestion here would be to call these variableshead and tail.

Ok, so you should have three files

Here is how things should flow for the addPerson function:



Here is how things should flow for the getPerson function:

*This is circular so if have 5 people in the address book and you call get person in a loop 25 times from main all five people will be returned five times.

Here is how the findPerson functions should flow:


Conceptually what you are creating is a circular queue that holds structs. Because you need to know where the next available spot in the array is you will need an index. This should be in the global section of addresBook.cpp. You are actually probably going to need two indexes, one to keep track of what is being added, and one that keeps track of what is being removed.

There are four functions and I can tell you that each of these can be written in about 5 or 6 lines of code.

What not to do

Using any of the following will drop your grade for this assignment by 70%

*If you want to create a function called printBook it is acceptable to have cout in that funciton but not in any others.

Explanation / Answer

Program for Address Book:

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);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote