Address book problem.

This is a really cliche problem, I know, but I'm still stumped. I have to read the first name, last name, phone number, etc. from an infile, but I'm confused because of all of the different classes and functions I have to use. I know it's a huge program to read through, but I'd be extremely grateful to anyone that helps!

Here's the function I'm having trouble with (in the main program):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void readFromFileIntoAddressBook(ifstream & infile, addressBookType& adBook)
{
	infile.open("infile.txt");
	if (!infile) {
		cout << "Error, could not read." << endl;
		system("pause");
	}
	while (!infile.eof()) {


		infile >> :personType(firstName) >> last;
		
	}

}


Here's the header file:

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
#include <string>
#include <iostream>
#include <fstream>
using namespace std;


///////////////////////////////////////////
// class personType
///////////////////////////////////////////  
class personType
{
public:
	void printInfo() const;
	//Function to output the first name and last name
	//in the form firstName lastName.

	void setInfo(string first, string last);
	//Function to set firstName and lastName according 
	//to the parameters.
	//Postcondition: firstName = first; lastName = last

	personType(string first = "DEFAULT FIRST NAME", string last = "DEFAULT LAST NAME");
	//constructor with all default parameters
	//Sets firstName and lastName according to the parameters.
	//The default values of the parameters are empty strings.
	//Postcondition: firstName = first; lastName = last  

private:
	string firstName; //variable to store the first name
	string lastName;  //variable to store the last name
};

///////////////////////////////////////////
// class addressType
///////////////////////////////////////////  
class addressType
{
public:
	void printInfo() const;
	void setInfo(string s, string z);

	addressType(string s = "DEFAULT STATE", string z = "DEFAULT ZIP");

private:
	string state;
	string zip;
};


///////////////////////////////////////////
// class extPersonType
///////////////////////////////////////////  

class extPersonType : public personType
{
public:

	void printInfo() const;

	void setInfo(string fName, string lName,
		string s, string z,
		string phone, string pStatus);

	string getStatus() const;

	extPersonType(string fName = "", string lName = "",
		string s = "", string z = "",
		string phone = "", string pStatus = "");

private:
	addressType address;
	string phoneNumber;
	string personStatus; // could be "Friend" , "Business", or "Family"
};


///////////////////////////////////////////
// class addresBookType
///////////////////////////////////////////  


const int ADDRESS_BOOK_SIZE = 500;

class addressBookType
{
public:
	void printInfo() const;
	void printInfoWithStatus(string status);

	// insert the eP to the end of the list, and return true.
	// But if the list is full  do not add eP instead just return false.
	bool insertLast(const extPersonType& eP);

	addressBookType();

private:
	extPersonType list[ADDRESS_BOOK_SIZE];
	int length; // actual number of entries in the list 
};


And here's the Implementation file:
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
void personType::printInfo() const
{
	cout << firstName << " " << lastName;
}

void personType::setInfo(string first, string last)
{
	firstName = first;
	lastName = last;
}

//constructor
personType::personType(string first, string last)

{
	firstName = first;
	lastName = last;
}


///////////////////////////////////////////
//  implementation of  class addressType
///////////////////////////////////////////  


void addressType::printInfo() const
{
	cout << state << ", " << zip << endl;
}

void addressType::setInfo(string s, string z)
{
	state = s;
	zip = z;
}


addressType::addressType(string s, string z)
{
	state = s;
	zip = z;
}


///////////////////////////////////////////////////////////////////
//  implementation of member functions of class extPersonType
///////////////////////////////////////////////////////////////////

void extPersonType::printInfo() const{
	personType::printInfo();
	address.printInfo();
	cout << phoneNumber << ", " << personStatus << endl;
}

void extPersonType::setInfo(string fName, string lName,
	string s, string z,
	string phone, string pStatus)
{
	personType::setInfo(fName, lName);
	address.setInfo(s, z);
	phoneNumber = phone;
	personStatus = pStatus;
}

string extPersonType::getStatus() const{
	return personStatus;
}

extPersonType::extPersonType(string fName, string lName,
	string s, string z,
	string phone, string pStatus)
	:personType(fName, lName), 
	address(s, z)
{
	
	phoneNumber = phone;
	personStatus = pStatus;
}



///////////////////////////////////////////////////////////////////
//  implementation of member functions of  class addresbooktype
///////////////////////////////////////////////////////////////////
void addressBookType::printInfo() const
{
	for (int i = 0; i < length; i++){
		list[i].printInfo();
	}
}

void addressBookType::printInfoWithStatus(string status)
{
	for (int i = 0; i < length; i++) 
		if (list[i].getStatus() = status)
		{ 
			list[i].printInfo();
			cout << endl;
		}
	
}

addressBookType::addressBookType(){
	length = 0;
}
Okay! I fixed addressBookType::printInfowithStatus by making the if statement.....

if (list[i].getStatus() == status)

.....and made my showMenu function!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void showMenu()
{
	int input;
	string status;
	addressBookType information;
	cout << "Please select from the following options, by number: " << endl;
	cout << endl;
	cout << "(1): Print the entire address book." << endl;
	cout << "(2): Print the information of someone with a particular status." << endl;
	cout << "(3): Exit the program." << endl;
	cin >> input;

	switch (input) {
	case '1':
		information.printInfo();
	
	case '2':
		cout << "Enter the status of the person (family, friend, or business):" << endl;
			cin >> status;
			information.printInfoWithStatus(status);
	case '3':
		system("pause");
	}
}


Now all that's left is to get the information from the infile correctly. Again, any help would be really appreciated!
can you show us the file format ? i mean the structure of the file you are reading from !
Last edited on
Sure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Shelly Malik 
Nebraska 
68131 
402-555-1212 
Family 

Donald Duck 
Florida 
11234 
622-873-8920 
Friend 

Chelsea Tomak 
Nebraska 
68172 
402-777-8888 
Business
ok , what are you trying to read the file into ? Vector or Arrays ?
Last edited on
I believe it's the arrays. Like I said, it's really making me confused because of all of the classes, inheritance rules, functions, arrays, etc.
ok, to read a file into an array , use a loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Declare variables to hold the file contents
        string firstName;
	string lastName;
	string state;
	string zipCode;
	string phoneNumber;
	string status;
// use a while loop to read the file into the variables
	while(infile >> firstName && infile >> lastName &&  infile >> state
         && infile >> zipCode && infile >> phoneNumber && infile >> status)
    {
        //Do something with the variables 

    }
Last edited on
I know this isn't correct, but is it something like this?

1
2
3
4
5
6
7
8
9
	
	while (infile >> firstName && infile >> lastName &&  infile >> state
		&& infile >> zipCode && infile >> phoneNumber && infile >> status)
	{
		personType.printInfo(firstName, lastName);
		addressType.printInfo(state, zipCode);
		extPersonType.printInfo(phoneNumber, status);

	}
Wait, I think I'm on to something! Since extPersonType:setInfo() contains all 6 variables, plus personType:setInfo() and address.setInfo(), I only have to call extPersonType.setInfo()! Am I right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
extPersonType Info;

	string firstName;
	string lastName;
	string state;
	string zipCode;
	string phoneNumber;
	string status;
	
	while (infile >> firstName && infile >> lastName &&  infile >> state
		&& infile >> zipCode && infile >> phoneNumber && infile >> status)
	{
		Info.setInfo(firstName, lastName, state, zipCode, phoneNumber, status);
	}
yes that is correct. it set up the information of a single person.
Last edited on
It compiles, but showMenu() isn't working correctly. No matter which number I put in, it just goes to "Press any key to continue...." Any more tips? Thanks a lot for helping me with this!

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
void showMenu()
{
	ifstream infile;
	addressBookType adBook;
	int input;
	string status;
	addressBookType information;
	cout << "Please select from the following options, by number: " << endl;
	cout << endl;
	cout << "(1): Print the entire address book." << endl;
	cout << "(2): Print the information of someone with a particular status." << endl;
	cout << "(3): Exit the program." << endl;
	cin >> input;

	switch (input) {
	case '1':
		readFromFileIntoAddressBook(infile, adBook);
		information.printInfo();
	
	case '2':
		readFromFileIntoAddressBook(infile, adBook);
		cout << "Enter the status of the person (family, friend, or business):" << endl;
			cin >> status;
			information.printInfoWithStatus(status);
	case '3':
		system("pause");
	}
}
Anyone?
input is an int. If you enter 1, 2 or 3 it will have the value 1, 2 or 3. It will not have the value '1', '2' or '3' which are integer values corresponding to the character's place in a character set.

Use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    switch(input) {
    case 1:
        // ...
        // don't forget to end each case with a break;
        break;

    case 2:
        //...
        break;

    case 3:
        // ...
        break;
    }


Last edited on
Topic archived. No new replies allowed.