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);
    }
}
darko
  • 2,438
  • 8
  • 42
  • 54

2 Answers2

2

Where you have:

struct arrayOfStructs;

You need:

struct contactType arrayOfStructs[200]; // assuming you want 200 structs 
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • 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| – darko Apr 02 '11 at 23:11
  • @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). – Bo Persson Apr 03 '11 at 09:48
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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203