我是StackExchange和C ++的新手,如果我不能很好地描述问题,请向我道歉。我需要一些家庭作业的帮助。 我试图找到加载此文件并存储其数据的方法。该文件具有不同的数据类型,因此我到目前为止所提出的是使用struct数组来存储所有值,使用三重嵌套for循环将数据保存在正确的变量中。
这是.dat文件的样子," comments"帮助描述正在发生的事情。
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 . . .
正如您所看到的,第一台机器的四分之一,硬币和镍币的数量是错误的,而第二台机器的型号名称是错误的。 如果有人有任何建议,将不胜感激。
答案 0 :(得分:0)
看起来你正在破坏数据。你的struct只分配一个模型和一个版本,但i索引的值为0和1.第二次读入这些时,地址会覆盖你的硬币数。