Homework help. Address book.

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
Ok, so you should have three files
main.cpp - holds you main function
addressBook.cpp - holds the address book functions that are required for the assignment
addressBook.h - which holds the definitions for addressBook.cpp

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "addressBook.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: " << endl;
			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 << 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)
				{

				}
			else
				{
					cout << "Sorry, that entry was not found " << endl;
				}
		}
	}
 
 
 
 
 
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;
					}
			}			
	}
 
 
void printStruct(struct person &print)
{
cout << print.first << " " << print.last << endl;
cout << print.address << endl;
}

Addressbook.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#pragma once
#ifndef ADDRESSBOOK
#define ADDRESSBOOK

const int MAXPEOPLE = 10;

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

class addressBook
{
private:
person p[MAXPEOPLE];
int count;
public:
addressBook();

addressBook(cString fName, cString lName, cString phone) {
   self.addPerson(fName, lName, phone);
}

bool addPerson(const person p);
bool addPerson(const char *fName,const char *lName, const char *phone);
bool getPerson(person &p);
bool getPerson(char *fName, char *lName, char *Phone);

};

#endif 


addressbook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include "addressBook.h"

using namespace std;

addressBook::addressBook()
: head(0), tail(-1)
{
    

}
addressBook::addressBook(const person &p)
: head(0), tail(-1)
{
    addPerson(p);


}
addressBook::addressBook(const person p[], int size)
: head(0), tail(-1)
{
    for(int i = 0; i < size; i++)
        addPerson(p[i]);

}
addressBook::addressBook(char *fName, char *lName, char *address)
: head(0), tail(-1)
{

    person tmp;
    strcpy(tmp.fName, fName);
    strcpy(tmp.lName,lName);
    strcpy(tmp.Address, address);
    addPerson(tmp);
}

bool addressBook::addPerson(const person &p)
{
    if(head < MAXADDRESS)
    {
        people[head] = p;
        head++;
        if(tail == -1)
            tail++;
        return true;
    }
    return false;
}
bool addressBook::getPerson(person &p)
{
    if(tail >=0)
    {
        if(tail >= MAXADDRESS)
            tail = 0;
        p = people[tail];
        tail++;
        return true;
    }
    return false;
}
bool addressBook::findPerson(char *lastName, person &p)
{
    for(int i = 0; i < head; i++)
    {
        if(!stricmp(people[i].lName, lastName))
        {
            p = people[i];
            return true;
        }
    }
    return false;
}
bool addressBook::findPerson(char *lastName, char *firstName, person &p)
{
    for(int i = 0; i < head; i++)
    {
        if(!stricmp(people[i].lName, lastName) && !stricmp(people[i].fName, firstName))
        {
            p = people[i];
            return true;
        }
    }
    return false;
}
void addressBook::printBook()
{

    for(int i = 0; i < head; i++)
    {
        cout << people[i].fName << "\t" << people[i].lName << "\t" << people[i].Address << endl;
    }

}
void addressBook::sort()
{
person temp;

     for(int i = 0; i < head; i++)
     {
         for (int j=0; j < head; j++)
         {
              if (stricmp(people[j+1].lName, people[j].lName) < 0)      
              { 
                    temp = people[j];           
                    people[j] = people[j+1];
                    people[j+1] = temp;
              }
          }
     }
}
 
ostream& operator<<(ostream& output, const Point& p) {
   output << "(" <<  p.x << ", " << p.y <<")";
   return output;  
}
 
bool& operator+= (const person p);
{
    return addPerson(const p);
}

person& operator() (int i)
{
    return people[i];
}
This is all me and a friend have so far. We have both been working on it, and aren't very good at programming. We are completely stumped now.
Hi,
I did a project like this over thanksgiving break. Yours is more complicated it seems but what is your question can you be more specific then "we are completely stumped?"
Hello,
Do you say mine is more complicated by the directions given or by the code we've attempted to write. It isn't working even though I believe it should. I will post the error I get, and where every underline is.
Main.cpp
25. cout & endl
35. cin
59. findPerson
69. findPerson
103. ==
109. cout & endl
122. the first == and findfirst
124. }
128. cout & endl
136. cout & endl
addressBook.h
22. All cString
23. self
addressBook.cpp has a ton of underlining too, but I am pretty sure that one was done right.
Errors
1
2
3
4
5
6
7
8
9
10
11
12
1
1>  main.cpp
1>c:\users\dane kron\documents\visual studio 2012\projects\addressbookc++\addressbookc++\main.cpp(1): warning C4627: '#include "addressBook.h"': skipped when looking for precompiled header use
1>          Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\dane kron\documents\visual studio 2012\projects\addressbookc++\addressbookc++\main.cpp(139): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>  addressBook.cpp
1>c:\users\dane kron\documents\visual studio 2012\projects\addressbookc++\addressbookc++\addressbook.cpp(1): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1>          Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\dane kron\documents\visual studio 2012\projects\addressbookc++\addressbookc++\addressbook.cpp(2): warning C4627: '#include "addressBook.h"': skipped when looking for precompiled header use
1>          Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\dane kron\documents\visual studio 2012\projects\addressbookc++\addressbookc++\addressbook.cpp(126): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Just by the directions. And your code. As far as your errors go I''m a beginner myself. I'm not very sure about your errors I thought you were having something more around my thinking. It is a similar project though:)
Sorry I can't be of more help. Did you at least Google your warnings and errors?
Topic archived. No new replies allowed.