I am having a problem with the second for loop that is reading data in to my structure. I have the first for loop reading in data for equipment but each piece of equipment has parts that go with it and that is captured in the second for loop and put into the Parts structure. The code all works good except that once the binary file is complete, the only data I have in the Parts structure is from the last loop of the "Y" for loop. I thought about nesting the Parts structure inside of the Record structure but could not figure out the code to accomplish that task. Surely could use some help here. I actually tried to nest it and kept getting errors because of the way I used the fread. It looked something like this:
fread(&Test[x].Each[y], sizeof(struct Record), 1, f_Ptr);
inside of the Record structure I added -struct Parts Each;-
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
#using <mscorlib.dll>
#define NUM 6
struct Parts
{
long int s_PtNm;
char s_PtDs[25];
float s_PtCt;
int s_Used;
};
struct Record
{
int s_Nums;
char s_Desc[25];
float s_Cost;
float s_Time;
int s_Made;
int s_Part;
};
int main()
{
FILE *f_Ptr;
struct Record Test[NUM];
struct Parts Each[NUM];
float a_Cost_Piece [6];
float a_Cost_Parts [6];
float a_Cost_Assembly[6];
float a_Cost_Produced[6];
float f_Item_Cost = 0;
float f_Parts_Cost = 0;
float f_Cost_Assembly = 0;
f_Ptr = fopen("V.DAT","rb");
for (int x = 0; x < NUM; x++)
{
fread(&Test[x], sizeof(struct Record), 1, f_Ptr);
printf("\n Product Stock Number: %i ", Test[x].s_Nums);
printf("\n Product Description : %s ", Test[x].s_Desc);
printf("\n Other Material Cost : %.2f ", Test[x].s_Cost);
printf("\n Assembly Line Time : %.2f ", Test[x].s_Time);
printf("\n Total Stock produced: %i ", Test[x].s_Made);
printf("\n Number of Parts Used: %i \n", Test[x].s_Part);
***below here is the area of concern***
for(int y = 0; y < Test[x].s_Part; y++)
{
***Problem here***
fread(&Each[y], sizeof(struct Parts), 1, f_Ptr);
printf("\n\tStock Part Number: %d ", Each[y].s_PtNm);
printf("\n\tParts Description: %s ", Each[y].s_PtDs);
printf("\n\tStock Part Cost : %.2f ", Each[y].s_PtCt);
printf("\n\tNumber Needed : %i \n", Each[y].s_Used);
a_Cost_Piece[x] = Each[y].s_PtCt * Each[y].s_Used;
a_Cost_Parts[x] += a_Cost_Piece[x];
}
f_Cost_Assembly = (a_Cost_Parts[x] + Test[x].s_Cost + (Test[x].s_Time * .1141));
a_Cost_Assembly[x] = f_Cost_Assembly + (f_Cost_Assembly * .16);
a_Cost_Produced[x] = a_Cost_Assembly[x] * Test[x].s_Made;
printf("\n Parts cost = $%.2f", a_Cost_Parts [x]);
printf("\n Assembly cost = $%.2f", a_Cost_Assembly[x]);
printf("\n Produced cost = $%.2f", a_Cost_Produced[x]);
printf("\n __________________________________________\n\n");
}
fclose(f_Ptr);
return 0;
}
fread(&Test[x].Each[y], sizeof(struct Record), 1, f_Ptr);
inside of the Record structure I added -struct Parts Each;-
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
#using <mscorlib.dll>
#define NUM 6
struct Parts
{
long int s_PtNm;
char s_PtDs[25];
float s_PtCt;
int s_Used;
};
struct Record
{
int s_Nums;
char s_Desc[25];
float s_Cost;
float s_Time;
int s_Made;
int s_Part;
};
int main()
{
FILE *f_Ptr;
struct Record Test[NUM];
struct Parts Each[NUM];
float a_Cost_Piece [6];
float a_Cost_Parts [6];
float a_Cost_Assembly[6];
float a_Cost_Produced[6];
float f_Item_Cost = 0;
float f_Parts_Cost = 0;
float f_Cost_Assembly = 0;
f_Ptr = fopen("V.DAT","rb");
for (int x = 0; x < NUM; x++)
{
fread(&Test[x], sizeof(struct Record), 1, f_Ptr);
printf("\n Product Stock Number: %i ", Test[x].s_Nums);
printf("\n Product Description : %s ", Test[x].s_Desc);
printf("\n Other Material Cost : %.2f ", Test[x].s_Cost);
printf("\n Assembly Line Time : %.2f ", Test[x].s_Time);
printf("\n Total Stock produced: %i ", Test[x].s_Made);
printf("\n Number of Parts Used: %i \n", Test[x].s_Part);
***below here is the area of concern***
for(int y = 0; y < Test[x].s_Part; y++)
{
***Problem here***
fread(&Each[y], sizeof(struct Parts), 1, f_Ptr);
printf("\n\tStock Part Number: %d ", Each[y].s_PtNm);
printf("\n\tParts Description: %s ", Each[y].s_PtDs);
printf("\n\tStock Part Cost : %.2f ", Each[y].s_PtCt);
printf("\n\tNumber Needed : %i \n", Each[y].s_Used);
a_Cost_Piece[x] = Each[y].s_PtCt * Each[y].s_Used;
a_Cost_Parts[x] += a_Cost_Piece[x];
}
f_Cost_Assembly = (a_Cost_Parts[x] + Test[x].s_Cost + (Test[x].s_Time * .1141));
a_Cost_Assembly[x] = f_Cost_Assembly + (f_Cost_Assembly * .16);
a_Cost_Produced[x] = a_Cost_Assembly[x] * Test[x].s_Made;
printf("\n Parts cost = $%.2f", a_Cost_Parts [x]);
printf("\n Assembly cost = $%.2f", a_Cost_Assembly[x]);
printf("\n Produced cost = $%.2f", a_Cost_Produced[x]);
printf("\n __________________________________________\n\n");
}
fclose(f_Ptr);
return 0;
}