【发布时间】:2022-08-08 15:47:29
【问题描述】:
我必须有一个从文本文件中读取卡片信息的函数 (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;
}
}
-
你有什么尝试\"使用指针将它们插入主程序中的并行数组\"?
-
附带说明一下,任何时候你想使用多个并行数组,你可能应该有一个数组,而不是它的元素类型是一个结构/类来保存数组中每个索引的所有信息。
-
@Wind 1)这个话题太大了,2)你怎么在没有教过指针的情况下被分配了一个涉及指针的作业?
-
@john 我被教过指针,但我不知道从哪里开始,那是没有知识的意思,因为指针教我一个值或多个值,当直到数组时,我很模糊......给您带来不便敬请见谅,谢谢回复!!感谢你的帮助!