Problem with passing an array of structures

I can’t think about how to pass this array of structures throughout my program. Can anyone lend a hand? Now I get an error message that says: expected primary expression before the marker ")".

Title:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

#include <iostream>
#include <fstream>
#include <string>
#include  <cstring>
#include <iomanip>
#include <cctype>

using namespace std;

struct addressType
{
    char streetName[36];
    char cityName[21];
    char state[3];
    char zipcode[6];
    char phoneNumber[15];
};

struct contactType
{
    char contactName[31];
    char birthday[11];
    addressType addressInfo;
    string typeOfentry;
};

typedef struct contactType contactInfo;

void extern readFile(ifstream&, int&, struct contactType *arrayOfStructs);
void extern sortAlphabetically();
void extern userInput(char&);
void extern output(char&);



#endif // HEADER_H_INCLUDED

Main:

#include "header.h"

int main()
{
    ifstream inFile;
    char response;
    int listLength;
    struct arrayOfStructs;

    inFile.open("AddressBook.txt");

    if (!inFile)
    {
        cout << "Cannot open the input file."
             << endl;
            return 1;
    }

    readFile(inFile, listLength, arrayOfStructs);
    sortAlphabetically();
    userInput(response);
    output(response);

    return 0;
}

ReadFile:

#include "header.h"

void readFile(ifstream& inFile, int& listLength, struct arrayOfStructs[])
{
    contactInfo arrayOfStructs[listLength];
    char discard;

    inFile >> listLength;
    inFile.get(discard);

    for (int i = 0; i < listLength; i++)
    {
        inFile.get(arrayOfStructs[i].contactName, 30);
        inFile.get(discard);
        inFile.get(arrayOfStructs[i].birthday, 11);
        inFile.get(discard);
        inFile.get(arrayOfStructs[i].addressInfo.streetName, 36);
        inFile.get(discard);
        inFile.get(arrayOfStructs[i].addressInfo.cityName, 21);
        inFile.get(discard);
        inFile.get(arrayOfStructs[i].addressInfo.state, 3);
        inFile.get(discard);
        inFile.get(arrayOfStructs[i].addressInfo.zipcode, 6);
        inFile.get(discard);
        inFile.get(arrayOfStructs[i].addressInfo.phoneNumber, 15);
        inFile.get(discard);
        inFile >> arrayOfStructs[i].typeOfentry;
        inFile.get(discard);
    }
}
+2
source share
2 answers

Where do you have:

struct arrayOfStructs;

You need:

struct contactType arrayOfStructs[200]; // assuming you want 200 structs 
+2
source

Arrays (structures or other things) suffer from many special rules, such as "attenuation" in the pointer at the slightest provocation (and, thus, forgetting about its length).

If you need a collection of 200 contactType, the easiest way is to use std :: vector

std::vector<contactType>   Contacts(200);

You can then pass a link to this to functions that require contacts.

+1
source

All Articles