I have a program in class that reads a data file for a store and then prints them out on a prompt.
1 Suits 300 100 92
1 Coats 200 60 65
1 Shirts 1000 12 13
2 Dresses 400 60 65
2 Coats 185 184 200
2 Shoes 600 40 30
3 Jeans 200 40 35
3 Shoes 200 30 34
4 Jeans 300 42 43
The numbers being department, item name, quantity, cost to buy, and cost to sale.
#include <stdio.h>
#include <stdlib.h>
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
int main()
{
FILE *in;
char item[8];
int department, quantity, prev = 1,k=0;
float cost, market, cost2, mark2, total, totcost, totmark = 0, lowest;
if((in = fopen("blinn.dat", "r")) == NULL)
{
printf ("Can't open file blinn.dat\n");
system("pause");
exit(1);
}
else
{
printf ("\n\n\t\t\t\t Blinn Apparel Store");
printf ("\n\n\n\t\t Unit Cost Extended");
printf ("\n Quantity Cost Market Cost Market Lower Cost\n");
printf ("\nMens Dept");
int m=0;
while(fscanf(in, "%d %s %d %f %f", &department, item, &quantity, &cost, &market) != EOF)
{
if(department != prev)
{
lowest = min(totcost, totmark);
printf ("\n Total\t\t\t\t $%8.2f $%8.2f $%8.2f", totcost, totmark, lowest);
totcost = 0;
totmark = 0;
prev = department;
total += lowest;
switch (m)
{
case 0:
printf("\nLadies Dept");
break;
case 1:
printf("\nGirls Dept");
break;
case 2:
printf("\nBoys Dept");
break;
}
m++;
}
if (department == 1)
{
cost2 = cost * quantity;
mark2 = market * quantity;
printf ("\n%8s %4d %8.2f %4.2f %8.2f %8.2f ",
item, quantity, cost, market, cost2, mark2);
totcost = totcost + cost2;
totmark = totmark + mark2;
}
if (department == 2)
{
cost2 = cost * quantity;
mark2 = market * quantity;
printf ("\n%8s %4d %8.2f%8.2f %8.2f %8.2f ",
item, quantity, cost, market, cost2, mark2);
totcost = totcost + cost2;
totmark = totmark + mark2;
}
if (department == 3)
{
cost2 = cost * quantity;
mark2 = market * quantity;
printf ("\n%8s %4d %8.2f %5.2f %8.2f %8.2f ",
item, quantity, cost, market, cost2, mark2);
totcost = totcost + cost2;
totmark = totmark + mark2;
}
if (department == 4)
{
cost2 = cost * quantity;
mark2 = market * quantity;
printf ("\n%8s %4d %8.2f %5.2f %8.2f %8.2f ",
item, quantity, cost, market, cost2, mark2);
totcost = totcost + cost2;
totmark = totmark + mark2;
lowest = min(totcost, totmark);
printf ("\nTotal\t\t\t\t\t $%8.2f $%8.2f $%8.2f\n", totcost, totmark, lowest);
total += lowest;
}
}
}
printf("Inventory at lower cost\t\t\t\t\t $%8.2f\n", total);
system ("pause");
}a
Disregard the nonuniform spacing for each line; I was troubleshooting a large number I was inexplicably getting. I have the code done all nice and dandy and it all checks out, but my professor has given me half credit because of the if statements, saying if there were a large number of departments my code wouldn't be feasible. He said I could replace it with one statement and that threw me off because I've tried working it into the switch statement, but that obviously doesn't work because some of the clothing items don't appear.
I just can't seem to change things without the math going crazy. My first thought was to make a function for the multiple additions and multiplications to totmark, totcost, cost2, and mark2, but anytime I disrupt it, everything falls apart and I can't seem to put it back together.
This should be an easy fix, but I appreciate any help.
I think they expect you to make use of function.
You could also use Structures.
and create an array of structures (in
main()
), like thisAlso you could write a generic function to calculate:
Side Note:
You could use
enum
and add the following to function
calculate