Okay so I'm pretty sure I've completely destroyed this code. All I'm trying to do is send the user-inputted product info into a binary file to display when the user chooses the report option from the menu. I'm so confused though and I think I really screwed it up.
// ** Prototypes **
void heading(void);
int menu();
void add();
void report();
char getstring(char entry, char prompt[]);
int getint(int min, int max, char prompt[]);
double getreal(double min, double max, char prompt[]);
double total(double quantity, double amount);
double profit(double profit_cost, double profit_price);
void init_costs(double t[], int x);
void show_costs(double t[], int x);
void show(int prodnum, int prodtype, char d[], int prodquan, double cost, double price, double prod_price, double prod_cost, double prod_profit);
int menuerror(int min, int max, char prompt[]);
int prompt(char msg[]);
int quit(void);
void message(char msg[]);
char firstUpper(char *buf);
typedef struct Sports
{
int prodnum;
int prodtype;
char prodscrip;
int prodquan;
double cost;
double price;
} Sports;
/* ================================================================ */
// ** Functions **
int main()
{
int TRUE = 1;
int FALSE = 0;
int select, end = TRUE;
do
{
select = menu();
switch (select)
{
case 1: add(); break;
case 2: report(); break;
case 3:
case 4:
case 5: end = quit(); break;
default: message("\n\nPlease make a selection between 1 and 5.\a");
}
}
while (end);
return 0;
}
/* ================================================================ */
int menu()
{
int choice;
system("cls");
printf("Sierra Sporting Goods\n\n");
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\n");
choice = menuerror(1, 5, "Enter your selection");
return(choice);
}
/* ================================================================ */
void heading()
{
system("cls");
printf("Sierra Sporting Goods\n\n\n");
return;
}
/* ================================================================ */
void add()
{
double cost, price, prod_cost, prod_price, prod_profit;
double MINCOST;
double MAXCOST;
double MINPRICE;
double MAXPRICE;
int prodnum, prodtype, prodquan;
int MINPROD;
int MAXPROD;
int MINTYPE;
int MAXTYPE;
int MINQUAN;
int MAXQUAN;
char prodscrip;
double types[6];
char choice;
char line[1][6];
Sports s;
FILE *fp;
init_costs(types,6);
fp = fopen("limits.txt", "rt");
if (fp == NULL)
{
printf("File does not exist");
return;
}
else
{
fscanf_s(fp, "%d %d %d %d %d %d %lf %lf %lf %lf", &MINPROD, &MAXPROD, &MINTYPE, &MAXTYPE, &MINQUAN, &MAXQUAN, &MINCOST, &MAXCOST, &MINPRICE, &MAXPRICE);
fclose(fp);
}
fp = fopen("data.dat", "ab");
if (fp == NULL)
{
message("Error opening data file\a");
return;
}
do
{
heading();
s.prodnum = getint(MINPROD, MAXPROD, "product number");
s.prodtype = getint(MINTYPE, MAXTYPE, "product type");
s.prodscrip = getstring(prodscrip, "Enter the product description: ");
s.prodscrip = firstUpper(&prodscrip);
s.prodquan = getint(MINQUAN, MAXQUAN, "product quantity");
s.cost = getreal(MINCOST, MAXCOST, "product cost");
s.price = getreal(MINPRICE, MAXPRICE, "product price");
prod_cost = total(prodquan, cost);
prod_price = total(prodquan, price);
prod_profit = profit(prod_price, prod_cost);
types[prodtype] += prod_cost;
show(prodnum, prodtype, prodscrip, prodquan, cost, price, prod_price, prod_cost, prod_profit);
choice = prompt("Would you like to enter another product");
}
while (choice != 'N');
getchar();
show_costs(types,6);
return;
}
/* ================================================================ */
char firstUpper(char *buf)
{
while (*buf) // Continue as long as characters available.
{
while( isspace(*buf) || !isalpha(*buf) )
*buf++; // Skip all spaces until next character.
*buf = toupper(*buf); // Capitalize first character after space.
while(*buf && !isspace(*buf) )
*buf++; // Skip all characters until next space.
}
}
/* ================================================================ */
int getint(int min, int max, char item[])
{
int x;
printf("Enter the %s between %d and %d: ", item, min, max);
scanf("%d%*c", &x);
while (x < min || x > max)
{
message("\nError in range. Press Enter to continue.\a");
printf("Enter the %s between %d and %d: ", item, min, max);
scanf("%d%*c", &x);
}
return(x);
}
/* ================================================================ */
double getreal(double min, double max, char item[])
{
double y;
printf("Enter the %s between $%.2lf and $%.2lf: ", item, min, max);
scanf("%lf%*c", &y);
while (y < min || y > max)
{
message("\nError in range. Press Enter to continue.\a");
printf("Enter the %s between $%.2lf and $%.2lf: ", item, min, max);
scanf("%lf%*c", &y);
}
return(y);
}
/* ================================================================ */
double total(double quantity, double amount)
{
return (quantity * amount);
}
/* ================================================================ */
double profit(double profit_price, double profit_cost)
{
return (profit_price - profit_cost);
}
/* ================================================================ */
void init_costs(double *t, int x)
{
int z;
for (z = 1; z < x; z++)
*(t + z) = 0;
}
/* ================================================================ */
void show_costs(double *t, int x)
{
int z;
double total = 0;
system("cls");
printf(" Product Cost by Type\n\n");
printf("_Type_____________Cost_\n\n");
for (z = 1; z < x; z++)
{
printf(" %2d%20.2lf\n", z, *(t+z));
total += *(t+z);
}
printf("________________________\n");
printf("%23.2lf\n\n\n\n", total);
printf("Press ENTER to return to the Main Menu...");
getchar();
}
/* ================================================================ */
void message(char *msg)
{
printf("%s\n\n", *msg);
}
/* ================================================================ */
int prompt(char *msg)
{
int z;
do
{
printf("%s (Y/N)? ", msg);
z = toupper(getchar());
if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
}
while (z != 'Y' && z != 'N');
return(z);
}
/* ================================================================ */
int quit()
{
int TRUE = 1;
int FALSE = 0;
int z, end = TRUE;
z = prompt("\nEnd program\a");
if (z == 'Y')
{
system("cls");
message("\nSierra Sporting Goods Database closed successfully.");
end = FALSE;
}
return(end);
}
/* ================================================================ */
int menuerror(int min, int max, char item[])
{
int z;
printf("%s from %d to %d: ", item, min, max);
scanf("%d%*c", &z);
while (z < min || z > max)
{
message("\nError in range. Press Enter to continue.\a");
printf("%s from %d to %d: ", item, min, max);
scanf("%d%*c", &z);
}
return(z);
}
/* ================================================================ */
void show(int prodnum, int prodtype, char prodscrip, int prodquan, double cost, double price,
double prod_price, double prod_cost, double prod_profit)
{
printf("\n\n\nThe product number is ----------> %04d\n", prodnum);
printf("The product type is ------------> %d\n", prodtype);
printf("The product description is -----> %s\n", prodscrip);
printf("The quantity is ----------------> %d\n", prodquan);
printf("The cost is --------------------> $%.2lf\n", cost);
printf("The price is -------------------> $%.2lf\n\n", price);
printf("Total product price ----------> $%.2lf\n", prod_price);
printf("Total product cost -----------> $%.2lf\n", prod_cost);
printf("Total product profit ---------> $%.2lf\n\n\n\n", prod_profit);
return;
}
/* ================================================================ */
void report()
{
int data;
FILE *fp;
struct Sports s;
fp = fopen(“data.dat”, “ab”);
while (fread(&s, sizeof(s), 1, fp) != 0)
{
show();
}
fclose(fp);
}