I have a very specific problem I am trying to solve. I have an array of structs containing product information. Each struct has the following information.
Supply type product name wholesale price quantity of wholesale retail price retail product quantity
My array of structs is complete and that is fine and dandy. Now, let's say there are a couple of supply types such as:
Meat Dairy Fruit etc...
How would I iterate through the array of structs and print out the value information according to supply type. And I am in need of a general solution. Because of course, there may be many product types.
Anything would help at this point. I am confused on how to do this and would greatly appreciate it if someone could help me out. I'm just not seeing how to do this or I am missing something.
EDITED Well, I changed my strategy a bit I am almost done and have everything working, there is just one minor glitch that I cannnot find. It's probably something fairly easy that I'm overlooking. Here is my updated code. If the supply type from the file is already in the ptr, just update the numerical values, if not make a record and add it to ptr. In my file I have this:
Meat Sirloin 3.55 15 7.30 8 Meat Chicken 2.51 9 5.44 5 Meat Bacon 3.30 23 4.38 10 Fruit Apple .50 40 1.11 20 Fruit Bananna .39 25 .85 16 Dairy Milk 1.00 25 2.25 15 Dairy Milk 1.00 25 2.25 15
It totals correctly for the meat category but anything after that it does not! I keep thinking this has to somehow do with the loop where I check if the supType is already there.
Here is my complete code.
void calculateDisplay(pointerDynam, sizeA);
void cleanUp();
typedef struct
{
char supType[15];
char prodName[15];
double wholePrice;
int quantWhole;
double retPrice;
int retProdQuantity;
} PRODUCT;
FILE *fr;
int main()
{
char supplyName[15];
char productName[15];
double wholeP = 0;
int quantityWhole = 0;
double retailPrice = 0;
int retailProductQuant = 0;
//keep track of supply types.
PRODUCT *ptr;
ptr = malloc(sizeof(PRODUCT));
PRODUCT *temp;
//int num =0;
int i = 1;
int num = 0;
int a;
int countTrack = 0;
bool alreadySupply = true;
int needsChanged = 0;
fr = fopen("ttt.txt", "r");
while(fscanf(fr, "%s %s %lf %d %lf %d", supplyName, productName, &wholeP, &quantityWhole, &retailPrice, &retailProductQuant)==6)
{
if(num != 0)
{
for(a=0; a < num; a++)
{
if(strcmp(ptr[a].supType, supplyName) == 0)
{
needsChanged = a;
}
else
{
alreadySupply = false;
}
}
}
if(num == 0 || alreadySupply == false)
{
PRODUCT record;
strcpy(record.supType, supplyName);
strcpy(record.prodName, productName);
record.wholePrice = wholeP;
record.quantWhole = quantityWhole;
record.retPrice = retailPrice;
record.retProdQuantity = retailProductQuant;
ptr[num] = record;
countTrack++;
num++;
i++;
temp = realloc(ptr, i*sizeof(PRODUCT));
ptr = temp;
}
else
{
ptr[needsChanged].quantWhole += quantityWhole;
ptr[needsChanged].retPrice += retailPrice;
ptr[needsChanged].retProdQuantity += retailProductQuant;
ptr[needsChanged].wholePrice += wholeP;
}
}
calculateDisplay(ptr, num);
cleanUp();
return 0;
}
void calculateDisplay(PRODUCT *pointerDynam, int sizeA)
{
int j;
double totownerCost = 0;
double totcustCost = 0;
double totprofit = 0;
double supownerCost = 0;
double supcustCost = 0;
double suprofit = 0;
for(j=0; j<sizeA; j++)
{
supownerCost = pointerDynam[j].wholePrice;
supcustCost = pointerDynam[j].retPrice;
suprofit = pointerDynam[j].retPrice - pointerDynam[j].wholePrice;
printf("Supply Type: %s\n Wholesale Price: %.2f\n Retail Price: %.2f\n Profit: %.2f\n\n\n",
pointerDynam[j].supType, supownerCost, supcustCost, suprofit);
totownerCost += pointerDynam[j].wholePrice;
totcustCost += pointerDynam[j].retPrice;
totprofit += pointerDynam[j].retPrice - pointerDynam[j].wholePrice;
}
printf("Wholesale Cost is: %.2f\n Retail is: %.2f\n Profit made was: %.2f\n\n", totownerCost, totcustCost, totprofit);
}
void cleanUp()
{
fclose(fr);
}