[英]Loading different data types from text file
I'm new to StackExchange and C++, so apologies if I don't describe the problem well enough. 我是StackExchange和C ++的新手,因此,如果我对问题的描述不够好,则表示歉意。 I need some help with homework. 我需要一些作业帮助。 I'm trying to find a way to load this file and store its data. 我正在尝试找到一种加载此文件并存储其数据的方法。 The file has different data types so what I came up with so far was to use struct array to store all the values, using a triple nested for loop to save the data in the correct variable. 该文件具有不同的数据类型,因此到目前为止,我想到的是使用结构数组存储所有值,并使用三重嵌套的for循环将数据保存在正确的变量中。
Here is what the .dat file looks like, with "comments" to help describe what's going on. .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
Here is the code I came up with 这是我想出的代码
#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;
}
Here is my test output. 这是我的测试输出。
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 . . .
As you can see, the number of quarters, dimes, and nickels is wrong for the first machine, and the model name is wrong for the 2nd model. 如您所见,对于第一台机器,四分之一,一角硬币和镍币的数量是错误的,而对于第二台机器,型号名称是错误的。 If anyone has any suggestions it would be much appreciated. 如果有人有任何建议,将不胜感激。
It looks like you are clobbering data. 看来您正在破坏数据。 Your struct allocates only one model and one version, but the i index takes values of 0 and 1. The second time these are meant to be read in, the address is overwriting your coin counts. 您的结构仅分配一个模型和一个版本,但i索引的值为0和1。第二次读取这些值时,该地址将覆盖您的硬币计数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.