How do I pass an array of struct to a multiple functions?

Viewed 74

I have three files. The struct to defined in the header file:

struct Person
    {
    string name;
    char gender;
    int age;
    };

The second file called functions, contains 2 function. A simple read from a text file:

/*PersonCountVal is a value from another function that counts 
how many records there are in the txt file for the 'for loop'.*/

void PersonFileRead(int PersonCountVal, Person *PersonRecord) 
{
    ifstream PersonFile;
    PersonFile.open("person.txt");

    if (!PersonFile)
    {
        cerr << "File can't be opened or not found! " << endl;
        
        system("PAUSE");
        exit(1);
    }
    PersonFile.ignore(numeric_limits<streamsize>::max(), '\n'); 
    
//the value for PersonCountVal is 20 i.e there are 20 records in the text file
        for (int i = 0; i < PersonCountVal; i++) 
        {
            PersonFile >> 
            PersonRecord[i].name >> 
            PersonRecord[i].gender >> 
            PersonRecord[i].age >> 
        }
        PersonFile.close();

The second function, the print funtion is:

void PersonDetailPrint(int PersonCountVal , Person *PersonRecord)
{
    for (int i = 0; i < PersonCountVal; i++) 
    {
        cout << PersonRecord[i].name << "\t";
        cout << PersonRecord[i].gender << "\t";
        cout << PersonRecord[i].age << "\t";
    }
}

The third file , the main file, contains the call and other stuff.

Person PersonRec[PersonCount];
Person *pointerPersonRec[PersonCount] = &PersonRec[PersonCount];

//Function 1 
PersonFileRead(PersonCount, pointerPersonRec[PersonCount]);

//Function 2
PersonDetailPrint(PersonCount, pointerPersoRec[PersonCount]);

The Function File has:

#include "header.h"

And the Main File has:

#include "header.h"
#include "Functions.cpp"

The error I'm getting is:

[Error] array must be initialized with a brace-enclosed initializer

My IDE is Dev-C++ 5.11.

I don't know if the entire thing is wrong or just the error part because I got some solutions from a few questions previously asked and answered on stack overflow and tried to combine them but it didn't work.

0 Answers
Related