C++ Pointer and Structure

A structure person is defined in a program. An array of pointer to person, p is created for me to store up to 10 persons using DYNAMIC MEMORY.

I need to do the following tasks in the main():

1) Read person information in file (person.txt) and store to array p using DYNAMIC MEMORY. Set the spouse pointer for each person to NULL value first.

2) Perform marry operation for Mary and Tom. You can marry two person by setting their spouse pointer to point to each other (store address of one another). You need to go through the array to find Mary and Tom by comparing name using strcmp.

3) Print out the content in array p where you need to print every person variable pointed by array p. If a person spouse pointer is NULL value, then print “Not Married”, else print the spouse name. Output of the program is shown in below. Make sure your output is exactly the same.

Output:

Name:Mary
Phone:012-35678905
Money:20000
Spouse Name:Tom

Name:John
Phone:010-87630221
Money:16000
Spouse Name:Not Married

Name:Alice
Phone:012-90028765
Money:9000
Spouse Name:Marry

Name:Tom
Phone:019-76239028
Money:30000
Spouse Name:Not Married

Name:Pam
Phone:017-32237609
Money:32000
Spouse Name:Not Married


I can do (1), read the text file person.txt, which has the following content:

Mary <space> 012-35678905 <space> 20000
John <space> 010-87630221 <space> 16000
Alice <space> 012-90028765 <space> 9000
Tom <space> 019-76239028 <space> 30000
Pam <space> 017-32237609 <space> 32000


But I don't know how to do (2) and (3).

This is what I have done so far, based on the template provided with the question and that I'm not supposed to change the code.

#include <iostream> //>>>>>>> This part is the template given >>>>>>>
#include <cstdlib>
#include <fstream>

using namespace std;

struct person
{
char name[30];
char phone[15];
double money;
person *spouse;
};

int main()
{
person *p[10]; //<<<<<<<< This is the end of the template part <<<

ifstream inFile;
inFile.open("person.txt");

if (inFile.fail())
{
cout << "Error in opening the person.txt file" << endl;
}

char name[30], phone[15];
int money;
int number = 5;

for (int i = 0; i < number; i++)
{
person* h = new person;

inFile >> h->name >> h->phone >> h->money;
h->spouse = NULL;
p[i] = h;

cout << "Name:" << p[i]->name << endl;
cout << "Phone:" << p[i]->phone << endl;
cout << "Money:" << p[i]->money << endl;
cout << "Spouse Name:";
if (p[i]->spouse == NULL)
{
cout << "Not Married" << endl;
}
else
{
cout << p[i]->spouse->name << endl;
}
cout << endl;
}

cin.get();

system("pause");
return 0;
}



Can anyone help me how to perform marry operation for Marry and Tom?!

Last edited on

Break it into steps.

1) Look through the array to find Mary, and when you do, get and hold onto a pointer to Mary.
2) Look through the array to find Tom, and when you do, get and hold onto a pointer to Tom.
3) Look through the array to find Mary, and when you do, set her spouse pointer to be the same as the pointer you got in step 2.
4) Look through the array to find Tom, and when you do, set his spouse pointer to be the same as the pointer you got in step 1.
while the exercise is about pointers, so you need to do what it says, if you have an array, the index is effectively a pointer. if you determine that array[25] == 'mary' then 25 is just as good as pointer= &(array[25]) for practical applications, and a bit easier to manage.

Just something to keep in the back of your mind. I dislike exercises that force students to do things that don't make any sense. It looks like they want pointers (in some places not all) where it isnt necessary.
Last edited on
@Repeater Why do you start 3) with "Look through the array to find Mary", when you already obtained a pointer to Mary in 1) ?

(Ditto for Tom in steps 4 and 2).
@MikeyBoy

Because programmer35 is very inexperienced and thinking about things in this kind of logical, programmatic way is very new, so I wrote the steps to be as simple and similar as possible.

If programmer35 can improve the method to do it more efficiently, good for programmer35.
Topic archived. No new replies allowed.