简体   繁体   中英

C++ Pointer and Structure

I have to do the following tasks:

  1. Read information about individual persons in file person.txt (see below) and store to array p . Set the spouse pointer for each person to NULL value first.
  2. Perform marry operation for Mary and Tom . You can marry two people by setting their spouse pointer to point to each other (store address of one another).
  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 a NULL value, then print Not Married , else print the spouse name. The output of the program is shown below. Make sure your output is the same.

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

Mary        012-35678905    20000
John        010-87630221    16000
Alice       012-90028765    9000
Tom         019-76239028    30000
Pam         017-32237609    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:

#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 file!" << endl;
    exit(1);
}

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

for (int i = 0; i < number; i++)
{
    inFile >> name >> phone >> money;
    cout << "Name:" << name << endl;
    cout << "Phone:" << phone << endl;
    cout << "Money:" << money << endl;
    cout << "Spouse Name:" << endl;
    cout << endl;
}

cin.get();

system("pause");
return 0;
}

The expected output should be like this:

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

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

...

Be aware that this exercise shows outdated use of C++

First to your array p that you somewhat forgot to use. p[10] is an array of 10. But of 10 what ? of person* , so of pointers to persons.

This is very old-fashioned C++. If you follow a course on internet, change immediately, because nowadays, we'd use vector<person> , string and nullptr . If it's a class course, you have no choice, so let's go on...

Some hints, based on what you have already done

First simplify the reading loop and don't forget to set the pointer to NULL as requested in the question:

for (int i = 0; i < number; i++)
{
    person *r = new person;                     // allocate a new person 
    inFile >> r->name >> r->phone >> r->money;  // read data into the new person
    r->spouse = NULL;                           // initialize the poitner
    p[i] = r;                                   // store the pointer in the array 
}

You already almost have the printing part(3). You just have to move it from your reading loop to a new loop, print from the array, and tackle the special case of married people:

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

Now something to do on your own

Now about marrying Marry and Tom in (2). This is more delicate. I will not do it for you, because now you have all you need to finish homework. But the general principle is:

  • Create two pointers spouse1 and spouse2 and initialize them to NULL.
  • Loop through the array to find which person is Tom and which one is Marry , and update the relevant pointer (eg spouse1 = p[i]; )
  • At the end of the loop, check that we have found both spouses (both pointers are not NULL anymore, and both pointers are different, because you cannot marry someone with hi/her-self)
  • If it's ok, then just marry them: spouse1->spouse=spouse2; spouse2->spouse=spouse1; spouse1->spouse=spouse2; spouse2->spouse=spouse1;

Finally, before you end the programme, you need to deallocate all the pointers in the array (with vectors, you wouldn't have to care about this).

Further improvements needed

You still need to improve your reading loop, to make it more dynamic. Because in reality, you don't know how many lines are in the text file. So start with number=0 and read data as long as possible, incrementing number each time, but stoping if not possible to read anymore, or if the maximum size of the array is reached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM