我是StackExchange和c++的新手,所以如果我没有很好地描述问题,请道歉。我需要一些家庭作业的帮助。我正在寻找一种方法来加载这个文件并存储它的数据。文件有不同的数据类型,所以我到目前为止想到的是使用struct数组来存储所有的值,使用三重嵌套的for循环将数据保存在正确的变量中。
这是。dat文件的样子,用"注释"来帮助描述发生了什么。
100A 2 // model number, 2 different versions of that model
0 0 0 // number of quarters, dimes, and nickels
5 //number of items in the vending machine
1A 1034 5 // Code combonation, ID Number, Quantity
1B 1000 10
1C 1100 10
1D 1123 20
1E 1222 5
0 0 0 // number of quarter, dimes, nickels in 2nd model
7 // number of items in the second version of that model
1A 2180 20
1B 1283 20
1C 3629 5
1D 3649 3
1E 4051 15
1F 4211 1
1G 5318 5
100B 3 // New model, with 3 different versions of itself.
2 10 5 //everything repeats like model 100A
7
这是我想出来的代码
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct VMdata
{
ifstream inFile;
string model[1];
int version[1];
int q[5];
int d[5];
int n[5];
int size[5];
string id[30];
int code[30];
int num[30];
char dummy;
};
int main()
{
VMdata New;
cout << fixed << setprecision(2) << showpoint;
New.inFile.open("machines.dat");
cout << "Model Data"
<< endl << endl;
int count1 = 0;
int count2= 0;
int count3 = 0;
for (int i = 0; i < 2; i ++)
{
cout << "i :" << count1 << endl;
New.inFile >> New.model[i] >> New.version[i]; // loads model number and number of versions i times
count1 = count1 + 1;
for (int j = 0; j < New.version[count1 -1]; j ++)
{
cout <<"j :" << count2 << endl;
New.inFile >> New.q[count2] >> New.d[count2] >> New.n[count2] >> New.size[count2]; // loads number of q, d, n, j times
count2 = count2 + 1;
for (int k = 0; k < New.size[count2 - 1]; k++)
{
cout << "k :" << count3 << endl;
New.inFile >> New.id[count3] >> New.code[count3] >> New.num[count3]; // loads id number, code number, and total number k times
count3 = count3 + 1;
}
}
}
New.inFile.close();
count1 = 0;
count2= 0;
count3 = 0;
cout << endl;
for ( int i = 0; i < 2; i ++)
{
cout << New.model[i] << setw(12) << New.version[i] << endl << endl;
count1 = count1 + 1;
for (int j = 0; j < New.version[count1 -1]; j ++)
{
cout << New.q[count2] << setw(12) << New.d[count2] << setw(12) << New.n[count2] << endl << setw(12) << New.size[count2] << endl;
count2 = count2 + 1;
for (int k = 0; k < New.size[count2 - 1]; k++)
{
cout << New.id[count3] << setw(12) << New.code[count3] << setw(12) << New.num[count3] << endl << endl;
count3 = count3 + 1;
}
}
}
return 0;
}
这是我的测试输出。
100A 2
3 15 0 // should be 0, 0, 0
5
1A 1034 5
1B 1000 10
1C 1100 10
1D 1123 20
1E 1222 5
0 0 0
7
1A 2180 20
1B 1283 20
1C 3629 5
1D 3649 3
1E 4051 15
1F 4211 1
1G 5318 5
♥ ☻ 3 // the heart and smile should be 100B lol
2 10 5
7
1A 2180 10
1B 1283 10
1C 3629 5
1D 3649 3
1E 4051 15
1F 4211 10
1G 3026 5
5 6 3
6
1A 6626 5
1B 6155 5
1C 5982 10
1D 5573 3
1E 5454 10
1F 5336 50
10 10 10
5
1A 1034 5
1B 1000 5
1C 1100 5
1D 1123 5
1E 1210 12
Press any key to continue . . .
如您所见,第一台机器的25分硬币、1角硬币和5分硬币的数量是错误的,第二台机器的型号名称是错误的。如果有人有任何建议,我将不胜感激。
看起来你在篡改数据。您的结构只分配一个模型和一个版本,但是i索引的值为0和1。第二次读取这些地址时,该地址将覆盖您的硬币计数。