[英]Need help reading a text file into parallel arrays
我必須有一個從文本文件(cards.txt)中讀取卡片信息並使用指針將它們插入主程序中的並行數組的函數。 我已成功讀取文本文件,但無法成功將信息插入數組。 任何人都可以幫助我嗎?
#include <iostream>
#include <stream>
#include <string>
using namespace std;
void readCards();
int main() {
ifstream inputFile;
const int SIZE = 10;
int id[SIZE];
string beybladeName[SIZE];
string productCode[SIZE];
string type[SIZE];
string plusMode[SIZE];
string system[SIZE];
readCards();
return 0;
}
void readCards() {
ifstream inputFile;
const int SIZE = 10;
int id[SIZE];
string beybladeName[SIZE];
string productCode[SIZE];
string type[SIZE];
string plusMode[SIZE];
string system[SIZE];
int i = 0;
inputFile.open("cards.txt");
cout << "Reading all cards information..." << endl;
if (inputFile) {
while (inputFile >> id[i] >> beybladeName[i] >> productCode[i] >> type[i] >> plusMode[I] >>
system[I]) {
i++;
}
cout << "All cards information read." << endl;
}
inputFile.close();
for (int index = 0; index < SIZE; index++) {
cout << "#:" << id[index] << endl;
cout << "Beyblade Name: " << beybladeName[index] << endl;
cout << "Product Code: " << productCode[index] << endl;
cout << "Type: " << type[index] << endl;
cout << "Plus Mode: " << plusMode[index] << endl;
cout << "System: " << system[index] << endl;
cout << " " << endl;
}
}
主要問題是您有兩組數組,一組在main
中,一組在readCards
中。 您需要main
中的一組數組並將這些數組(使用指針)傳遞給readCards
。 像這樣
void readCards(int* id, string* beybladeName, string* productCode, string* type, string* plusMode, string* system);
int main()
{
ifstream inputFile;
const int SIZE = 10;
int id[SIZE];
string beybladeName[SIZE];
string productCode[SIZE];
string type[SIZE];
string plusMode [SIZE];
string system [SIZE];
readCards(id, beybladeName, productCode, type, plusMode, system);
return 0;
}
void readCards(int* id, string* beybladeName, string* productCode, string* type, string* plusMode, string* system)
{
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.