2

I cant for the life of me figure out how to pass this array of structs throughout my program. Could anyone lend a hand? Right now i am getting an error in main that says: expected primary expression before ')' token.

Header:

#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);
    }
}
0

2 Answers 2

2

Where you have:

struct arrayOfStructs;

You need:

struct contactType arrayOfStructs[200]; // assuming you want 200 structs 
Sign up to request clarification or add additional context in comments.

2 Comments

error: using typedef-name contactInfo' after struct'| In function `void readFile(std::ifstream&, int&, contactInfo*)':| error: declaration of 'contactInfo arrayOfStructs[((unsigned int)((int)listLength))]' shadows a parameter|
@Matt - you have a parameter and a local variable with the same name. The compiler is warning you that then you can't access the parameter (because its name is hidden behind the other one).
1

Arrays (of structs or other things) suffer from a lot of special rules, like "decaying" into a pointer at the slightest provocation (and thus forgetting its length).

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

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

You can then pass a reference to this to functions needing the contacts.

1 Comment

He can also use std::array/boost::array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.