I must have a function that reads card information from a text file (cards.txt) and insert them to parallel arrays in the main program using a pointer. I have successfully read the text file, but cannot successfully insert the info to the arrays. Can anyone help me please?
#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;
}
}
CodePudding user response:
The main problem is that you have two sets of arrays, one in main
, and one in readCards
. You need one set of arrays in main
and to pass those arrays (using pointers) to readCards
. Like this
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)
{
...
}