故障传递结构的数组 [英] Trouble passing an array of structs
本文介绍了故障传递结构的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不能为我的生活弄清楚如何通过我的整个程序数组结构的。任何人都可以伸出援助之手?现在,我得到的主要错误,上面写着:预计前原前pression')'令牌。
标题:
的#ifndef HEADER_H_INCLUDED
#定义HEADER_H_INCLUDED#包括LT&;&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;串GT;
#包括LT&; CString的GT;
#包括LT&;&了iomanip GT;
#包括LT&;&cctype GT;使用命名空间std;地址类型结构
{
焦炭streetName [36];
焦炭的cityName [21];
焦状态[3];
焦炭拉链code [6];
炭phoneNumber的[15];
};结构contactType
{
焦炭CONTACTNAME [31];
焦炭生日[11];
地址类型AddressInfo的;
串typeOfentry;
};typedef结构contactType CONTACTINFO;无效的extern READFILE(ifstream的&安培;,&INT放大器;,结构contactType * arrayOfStructs);
无效的extern sortAlphabetically();
无效的extern userInput(字符和放大器;);
无效的extern输出(焦&安培;);#ENDIF // HEADER_H_INCLUDED
主:
的#includeheader.h诠释的main()
{
ifstream的INFILE;
焦炭反应;
INT listLength;
结构arrayOfStructs; inFile.open(AddressBook.txt); 如果(!INFILE)
{
COUT<< 无法打开输入文件。
<< ENDL;
返回1;
} READFILE(INFILE,listLength,arrayOfStructs);
sortAlphabetically();
userInput(响应);
输出(响应); 返回0;
}
READFILE:
的#includeheader.h无效READFILE(ifstream的&安培; INFILE,INT和放大器; listLength,结构arrayOfStructs [])
{
CONTACTINFO arrayOfStructs [listLength]
焦炭丢弃; INFILE>> listLength;
inFile.get(丢弃); 的for(int i = 0; I< listLength;我++)
{
inFile.get(arrayOfStructs [I] .contactName,30);
inFile.get(丢弃);
inFile.get(arrayOfStructs [I] .birthday,11);
inFile.get(丢弃);
inFile.get(arrayOfStructs [I] .addressInfo.streetName,36);
inFile.get(丢弃);
inFile.get(arrayOfStructs [I] .addressInfo.cityName,21);
inFile.get(丢弃);
inFile.get(arrayOfStructs [Ⅰ] .addressInfo.state,3);
inFile.get(丢弃);
inFile.get(arrayOfStructs [I] .addressInfo.zip code,6);
inFile.get(丢弃);
inFile.get(arrayOfStructs [I] .addressInfo.phoneNumber,15);
inFile.get(丢弃);
INFILE>> arrayOfStructs [I] .typeOfentry;
inFile.get(丢弃);
}
}
解决方案
您有:
结构arrayOfStructs;
您需要:
结构contactType arrayOfStructs [200]; //假设你要200结构
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);
}
}
解决方案
Where you have:
struct arrayOfStructs;
You need:
struct contactType arrayOfStructs[200]; // assuming you want 200 structs
这篇关于故障传递结构的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文