我不能为我的生活弄清楚如何在我的程序中传递这个结构数组。任何人都可以伸出援手吗?现在我在main中遇到一个错误:在')'令牌之前预期的主表达式。
标题:
#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
主要
#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)
你在哪里:
struct arrayOfStructs;
你需要:
struct contactType arrayOfStructs[200]; // assuming you want 200 structs
答案 1 :(得分:1)
阵列(结构或其他东西)受到许多特殊规则的影响,例如在最轻微的挑衅中“腐烂”成指针(从而忘记它的长度)。
如果您需要200个contactType的集合,最简单的方法是使用std :: vector
std::vector<contactType> Contacts(200);
然后,您可以将此引用传递给需要联系人的函数。