Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define Company ("Sierra Sporting Goods")
typedef struct
{
double P[6];
int NUMBER, PRODUCT, QUANTITY;
double COST, PRICE;
char DESCRIPTION[18];
}INVENTORY;
void add();
void show(int NUMBER, int QUANTITY, double COST, double PRICE);
double total(double A, double B);
int getint(int MIN,int MAX, char prompt[]);
double getreal(double dMIN, double dMAX, char prompt[]);
int menu();
void show(INVENTORY rec, double COST, double PRICE, double profit, char DE[]);
void init_costs(double *P, int c);
void show_costs(double *P, char **DEPARTMENT, int c);
void gets(char entry[], char prompt[]);
void strcase(char *s);
void report();
char *DEPARTMENT[] = {"Camping", "Tennis", "Golf", "Snow Sports", "Water Sports"};
int main(void)
{
int key;
do
{
key = menu();
switch (key)
{
case 1: add(); break;
case 2: report(); break;
case 3:
case 4:
case 5: return 0; break;
default: printf("\nError choose again.");
}
}
while (key);
return 0;
}
int menu(void)/*Main Menu*/
{
int choice;
printf("\n%s\n", Company);
printf("1 = Add a record\n");
printf("2 = Report\n");
printf("3 = Delete a record\n");
printf("4 = Change a record\n");
printf("5 = Quit\n");
choice = getint(1, 5, "Enter your selection: \n ");
return(choice);
}
void add()/*Calls functions(main part of program)*/
{
double total_price, total_cost, profit;
char choice;
int readValues, items, c;
int rNUMMIN = 0, rNUMMAX = 0, rTYPEMIN = 0, rTYPEMAX = 0, rQUANTITYMIN=0, rQUANTITYMAX=0;
double rCOSTMIN=0, rCOSTMAX=0, rPRICEMIN = 0, rPRICEMAX = 0;
INVENTORY rec;
FILE *fp;
if ((fp = fopen("inventory.dat", "ab")) == NULL)
{
printf("Error opening file.");
exit(1);
}
FILE *fr;
if((fr = fopen("limits.txt", "r")) == NULL)
{
printf("File does not exist");
exit (1);
}
else{
readValues = fscanf(fr, "%d%d%d%d%d%d%lf%lf%lf%lf", &rNUMMIN, &rNUMMAX, &rTYPEMIN, &rTYPEMAX, &rQUANTITYMIN, &rQUANTITYMAX, &rCOSTMIN, &rCOSTMAX, &rPRICEMIN, &rPRICEMAX);
if(readValues != 10){
printf("Unable to get limits");
exit(1);
}
}
init_costs(rec.P, 6);
fseek(fp, 0L, SEEK_END);
c = (int)ftell(fp) / sizeof(rec);
do
{
rec.NUMBER = getint(rNUMMIN, rNUMMAX, "product number: \n\n");
rec.PRODUCT = getint(rTYPEMIN, rTYPEMAX, "product type: \n\n");
gets(rec.DESCRIPTION, "description: \n\n");
strcase(rec.DESCRIPTION);
rec.QUANTITY = getint(rQUANTITYMIN, rQUANTITYMAX, "Quantity: \n\n");
rec.COST = getreal(rCOSTMIN, rCOSTMAX, "product cost: \n\n");
rec.PRICE = getreal(rPRICEMIN, rPRICEMAX, "product price: \n\n");
total_cost = total(rec.COST, rec.QUANTITY);
total_price = total(rec.PRICE, rec.QUANTITY);
profit = total_price - total_cost;
rec.P[rec.PRODUCT] += total_price;
show_costs(rec.P, DEPARTMENT, 6);
show(rec, total_cost, total_price, profit, rec.DESCRIPTION);
printf("Enter another product: Y/N?");
scanf("%c%*c", &choice);
if (choice == 'y' || choice =='Y')
{
items = fwrite(&rec, sizeof(rec), 1, fp);
if (items == (int)NULL) printf("Error writing to file");
else c++;
}
}
while( choice == 'y' || choice == 'Y');
fclose(fr);
fclose(fp);
return;
}
int getint(int MIN, int MAX, char prompt[])/*Min Max statement for values of number product and quantity*/
{
int a;
printf("\nEnter the %s %d to %d: ", prompt, MIN, MAX);
scanf("%d%*c", &a);
while ( a < MIN || a > MAX)
{
printf("\nInvalid, try again.\n");
printf("\nEnter the %s %d to %d: ", prompt, MIN, MAX);
scanf("%d%*c", &a);
}
return (a);
}
double getreal(double dMIN, double dMAX, char prompt[])/*Function for cost and price*/
{ double a;
printf("Enter the %s %.2f to %.2f: ", prompt, dMIN, dMAX);
scanf("%lf%*c", &a);
while ( a < dMIN || a > dMAX)
{
printf("\nInvalid, try again.\n");
printf("Enter the %s %.2f to %.2f: ", prompt, dMIN, dMAX);
scanf("%lf%*c", &a);
}
return(a);
}
double total(double A, double B)/*Function to be called for total price cost and profit*/
{
double n;
double total_cost, total_price, profit;
total_cost = (A * B);
total_price = (A * B);
return (total_cost);
return (total_price);
return(n);
}
void init_costs(double *P, int c)/*Intializes chart to 0*/
{
int i;
for (i = 1; i < c; i++)
*(P + i) = 0;
}
void gets(char entry[], char prompt[])
{
printf("Enter the %s: ", prompt);
gets(entry);
}
void strcase(char *s)/*Uppercase the description*/
{
int i = 0;
*s = toupper(*s);
s++;
while (*s != NULL)
{
if (*s == ' ')
{
*(s+1) = toupper(*(s+1));
s++;
}
else
{
*s = tolower(*s);
}
s++;
}
}
void show_costs(double *P, char **DEPARTMENT, int c)/*Show costs by department*/
{
int i;
double total = 0;
printf("Total by category Department\n");
for (P++, i = 1; i < c; i++, P++, DEPARTMENT++)
{
printf("%2d%30s%20.2lf\n", i, *DEPARTMENT, *P);
total += *P;
}
printf("%52.2lf\n", total);
}
void show(INVENTORY rec, double total_cost, double total_price, double profit, char DESCRIPTION[])/*Show grand totals*/
{
printf("\nThe product number is--> %d.", rec.NUMBER);
printf("\nThe product type is----> %d.", rec.PRODUCT);
printf("\nThe description is-----> %4s.", rec.DESCRIPTION);
printf("\nThe quantity is--------> %d.", rec.QUANTITY);
printf("\nThe cost is------------> %.2lf.", total_cost);
printf("\nThe price is-----------> %.2lf.", total_price);
printf("\nThe profit is----------> %.2lf.", profit);
printf("\nPress Enter to continue");
getchar();
}
void report()
{
INVENTORY rec;
int c, num = 0;
double profit;
FILE *fp;
printf ("\n\n", Company);
fp = fopen("inventory.dat", "rb");
if (fp == NULL) { printf("Error opening file.\a");
return;
}
printf("Prod# Type Description Qty Cost Price Profit\n\n");
while (fread(&rec, sizeof(rec), 1, fp))
{
++num;
printf("%04d %d %s %d %.2lf %.2lf %.2lf\n\n", rec.NUMBER, rec.PRODUCT, rec.DESCRIPTION, rec.QUANTITY, rec.COST, rec.PRICE, profit);
}
printf("\nDone\n");
fclose(fp);
}