Question
  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.

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 Figure 1 below. Make sure your output is exactly the same

D\uccd1024\may2018 Assignment1\test-roject-jan2019\Debug\test-roject-jan2019.exe Name: Mary Phone: 012-35678905 Money: 20000

person.txt

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

app.cpp

#include <iostream>
#include <cstdlib>
#include <fstream>


using namespace std;

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

int main()
{
        person *p[10];
        

        system("pause");
        return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi i am answering your question hope this will be helpful to you , kindly give a thumbs up if it helps you and do comment if you have any doubt regarding the same.

Code:-

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string.h>


using namespace std;

typedef struct person //structure for the person i used typedef so i do not need to use keyword struct every where
{
char name[30];
char phone[15];
double money;
person *spouse;
}person;

int main()
{
person *p[10] ;
ifstream in("person.txt"); //Opening file
int recCount = 0; // initilize the record conter to 0 and will give the total no of entry in the file
while (!in.eof() )
{
p[recCount] = (person*)malloc(sizeof(person)); //providing size to the pointer
in >> p[recCount]->name >> p[recCount]->phone>>p[recCount]->money; // copying values into the corrosponding fields
p[recCount]->spouse = NULL; // initilizing spouse to NULL as said in the question
++recCount; // incresing rec count
}
for(int i = 0; i < recCount; i++)
{
if(strcmp(p[i]->name ,"Mary")==0)
{
  
for(int j = 0 ; j < recCount ; j++)
{
if(strcmp(p[j]->name , "Tom")==0)
{
p[i]->spouse = p[j]; // coping the address of the structer of TOM to spouse of Mary
p[j]->spouse = p[i]; //coping the address of the structer of Mary to spouse of Tom
}
}
}
cout<<"Name:"<<p[i]->name<<endl<<"Phone:"<<p[i]->phone<<endl<<"Money:"<<p[i]->money<<endl;
if(p[i]->spouse == NULL)
cout<<"Spouse Name:"<<"Not Married"<<endl<<endl;
else
cout<<"Spouse Name:"<<p[i]->spouse->name<<endl<<endl;
}

  
return 0;
}

output:-

Name:Mary Phone: 012-35678905 Money: 20000 Spouse Name:Tom Name: John Phone: 010-87630221 Money: 16000 Spouse Name:Not Marrie

Happy to help kindly give a thumbs up . and if still you have any doubt then you can comment.

Add a comment
Know the answer?
Add Answer to:
Read person information in file person.txt and store to array p using DYNAMIC MEMORY. Set the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT