How do you search for an element in a array of structures from a string?

I'm doing the addressbook by structures program(I'm sure many of you have seen it) and I'm stuck at the function where you search the global addressbook array for the right structure via a match from an string taken from the user. It always prints out a wall of jibberish and the first name here and there when I get to that portion of the program instead of just clearly printing out the info requested. What am I doing wrong here?

Side question: Part of the program involves using the getPerson function. I think I've done the function correctly but I'm wondering, what's the point of even having it? How do I implement it to main to be relevant?

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

struct person
{
	char fname[100];
	char lname[100];
	char address[100];

};

person * addressbook[10];

bool addPerson(person *nextperson);
bool getPerson(person *getperson);
bool findPerson(string name, person *findit);
void printBook(person *findit);


int nextfreespot = 0;


int main()
{

	person info;
	person getthegirl;
	person findher;
	string lastname;

	for (int i = 0; i < 10; i++)
	{

	cout << "Enter a first name, last name, and address" << endl;
	cin >> info.fname >> info.lname >> info.address;
	if (addPerson(&info) == false)
	{
		cout << "Addressbook is full" << endl;
	}



}
	
	
	
	cout << "To search for a person, enter their last name: " << endl;
	cin >> lastname;
	if (findPerson(lastname, &findher) == false)
	{
		cout << "Person not found" << endl;
	}
	else
	{
         // This part right here is where it should print clearly
		printBook(&findher);
	}



	return 0;
}
	



bool addPerson(person *nextperson)
{
	if (nextfreespot == 10)
	{
		return false;
	}
	else
	{
	addressbook[nextfreespot] = nextperson;
	nextfreespot++;
	return true;
	}
}
// What do I do with this function?
bool getPerson(person *getperson)
{
	if (addressbook[nextfreespot] == 0)
	{
		return false;
	}
	else
	{
	addressbook[nextfreespot] = getperson;
	return true;
	}
}
//Heres the troublemaker function
bool findPerson(string name, person *findit)
{
	for (int i = 0; i < 10; i++)
	{
		if (name == addressbook[i]->lname)
		{
			addressbook[i] = findit;
			return true;
		}
		else
			return false;
	}
}

void printBook(person *findit)
{
	cout << findit->fname << endl;
	cout << findit->lname << endl;
	cout << findit->address << endl;
}
You have line 101 backwards.
In addition to the above answer, you should also move that "return false" to be the last statement executed in your code.

This leads to the next problem in your code. When you are adding people to the address book, you are using the same person struct to add people. So when you have finished adding people, you have every pointer in the address book pointing at the same person (The last person added).

Not sure you need both a getPerson and findPerson functions. Just use findPerson
Here is my updated code as well as he instruction of the getPerson function follow:

"In main create a person struct
call getPerson passing the created struct by reference
If the addressBook is empty getPerson simply returns false
If the addressBook is not empty the next person in the addressBook is copied to the struct passed by reference and true is returned
*If the last person in the address book is returned back then the next call to getPerson will get the first person again."

What do I do with it? Did I even do it right?

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

#include <iostream>
#include <string>
using namespace std;

struct person
{
	char fname[100];
	char lname[100];
	char address[100];

};

person * addressbook[10];

bool addPerson(person *nextperson);
bool getPerson(person *getperson);
bool findPerson(string name, person *findit);
void printBook(person *findit);


int nextfreespot = 0;


int main()
{

	person * info[10];
	
	person findher;
	string lastname;

	

	for (int i = 0; i < 10; i++)
	{
		info[i] = new person;
		cout << "Enter a first name, last name, and address" << endl;
		cin >> info[i]->fname >> info[i]->fname >> info[i]->address;
		if (!addPerson(info[i]))
		{
			cout << "Addressbook is full" << endl;
		}

	}


	
	
	cout << "To search for a person, enter their last name: " << endl;
	cin >> lastname;
	if (findPerson(lastname, &findher) == true)
	{
		printBook(&findher);
	}
	else
	{
	cout << "Person not found" << endl;
	
	}

	return 0;
}
	




bool addPerson(person *nextperson)
{
	if (nextfreespot == 10)	
	{
	return false;
	}
	else
{
	addressbook[nextfreespot] = nextperson;
	nextfreespot++;
	return true;
	}
	}

bool getPerson(person *getperson)
{
	if (addressbook[nextfreespot] == 0)
	{
	return false;
	}
    else
	{
	addressbook[nextfreespot + 1] = getperson;
	return true;
	}
 }

bool findPerson(string name, person *findit)
{
	for (int i = 0; i < nextfreespot; i++)
	{
		if (name.compare(addressbook[i]->lname) == 0)
		{
			findit == addressbook[i];
			return true;
		}

	}
	
}

void printBook(person *findit)
{
	cout << findit->fname << endl;
	cout << findit->lname << endl;
	cout << findit->address << endl;
}
Last edited on
Line 102, you are using a comparison operator instead of assignment (== vs =). You also left out the return false; outside of the loop.

You fixed the issue that Simac89 pointed out, but there's no need to use an array of pointers instead of a just a pointer.

Your getPerson function is totally wrong.
Topic archived. No new replies allowed.