Code:
/*======================================================*/
void add( )
{
double array[6];
int choice, items, c;
double totcost, totprice, profit;
char *types[] = {"", "Camping ", "Tennis ", "Golf ", "Snow Sports", "Water Sports"};
data rec;
float MINC, MAXC, MINP, MAXP;
int MINN, MAXN, MINT, MAXT, MINQ, MAXQ;
FILE *file1;
FILE *file2 ;
file1 = fopen("/Users/jason/Documents/CS 46/Xcode Programs/Assignments/#9 ssgtextfiles/limits.txt", "rt");
if (file1 == NULL)
{
tryagain("Error opening file");
return;
}
while (fscanf(file1, "%d%d%d%d%d%d%f%f%f%f", &MINN, &MAXN, &MINT, &MAXT, &MINQ,
&MAXQ, &MINC, &MAXC, &MINP, &MAXP) != EOF)
fclose(file1);
file2 = fopen("data.dat", "ab");
if (file2 == NULL)
{
tryagain("Error opening file");
return;
}
// fseek(file2, 0L, SEEK_END); // Get current record count from file, assign to c
//c = (int)ftell(file2) / sizeof(rec);
init_costs(array, 6);
do
{
//if (more)
{
rec.number = getint("product number", MINN, MAXN);
rec.type = getint("product type", MINT, MAXT);
getstring(rec.s);
rec.quantity = getint("product quantity", MINQ, MAXQ);
rec.cost = getreal("product cost", MINC, MAXC);
array[rec.type] += (total(rec.quantity, rec.cost));
rec.price = getreal("product price", MINP, MAXP);
choice = getyn("\nAnother");
totcost = total(rec.quantity, rec.cost);
totprice = total(rec.quantity, rec.price);
profit = totprofit(totprice, totcost);
show(rec.number, rec.type, rec.s, rec.quantity, rec.cost, rec.price, totprice, totcost, profit);
if (choice == 'Y')
{
items = fwrite(&rec, sizeof(rec), 1, file2);
if (items == (int)NULL) tryagain("Error writing to file");
else c++;
}
}
}
while (choice != 'N');
//fwrite(&rec, sizeof(rec), 1, file2);
fclose(file2);
report(types, array, 6);
getchar( ); /* Note: Unsure of why I needed to place getchar() twice. It would not prompt me to press Enter otherwise*/
printf("\n\n");
printf("Press ENTER to continue\n");
getchar( );
return;
}
/*======================================================*/
void report2( )
{
data rec;
FILE *file2;
int num = 0;
double profit;
file2 = fopen("data.dat", "rb");
if (file2 == NULL)
{
tryagain("Error opening file");
return;
}
printf("Prod# Type Description Qty Cost Price Profit\n");
printf("=====================================================================\n");
while (fread(&rec, sizeof(rec), 1, file2))
{
++num;
profit = totprofit(rec.price, rec.cost);
printf("%4.f%4d%20s%9.f%10.2lf%10.2lf%10.2lf\n", rec.number, rec.type, rec.s, rec.quantity, rec.cost, rec.price, profit);
}
printf("=====================================================================\n");
printf("Total expected profit %10.2lf\n", /*.........NOT SURE WHAT TO PUT HERE......*/ );
fclose(file2);
}
/*======================================================*/