I'm new to StackExchange and C++, so apologies if I don't describe the problem well enough. 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.
Here is what the .dat file looks like, with "comments" to help describe what's going on.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.