Trouble passing an array of structs
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:
#ifnd开发者_如何学Cef 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);
}
}
Where you have:
struct arrayOfStructs;
You need:
struct contactType arrayOfStructs[200]; // assuming you want 200 structs
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.
精彩评论