Code:
/* Description: This program is to accept data entry and then display */
/* the results for a fictitious company. Multiple data */
/* entry to be accepted until operator quits. Data */
/* to be calculated and displayed. */
/* with validation: product number (1 - 9999) */
/* product type (1 - 5) */
/* quantity (1 -50) */
/* cost (5.00 - 900.00) */
/* price (6.00 - 1000.00) */
/* Add 7 functions: main(), menu(), add(), getreal(), */
/* getint(), total(), and show(). */
/* Include category array: 1 = Camping, 2 = Tennis, */
/* (with running totals) 3 = Golf, 4 = Snow sports, */
/* (display totals) 5 = Water sports */
/* Include product description: create new function */
/* getstring() that uses gets() for keyboard input, */
/* new function strcase() to convert desc. first */
/* letter of each word uppercase + balance lowercase, */
/* display new desc. in show(), create 2D array for */
/* showcosts() dept.name */
/* Convert init-costs() & show-costs() to use pointers */
/* Also, modify getstring() and all string functions */
/* Add limits.txt to be used for validation (remove old) */
/* */
/* Compiler: Microsoft Visual C++ 2010 Express (Windows XP) */
/* */
/*--------------------------------------------------------------------------*/
/* Disable warning messages 4996 - (scanf deprecated; use scanf_s) */
#pragma warning( disable : 4996 )
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define COMPANY "Sierra Sporting Goods" /* define company for potential multiple uses */
#define TRUE 1
int menu(void);
void add(void);
int getint(int min, int max, char prompt[]);
double getreal(double min, double max, char prompt[]);
double total(int q, double c);
void show(int prodNum, int prodType, int prodQuant, double prodCost, double prodPrice, char desc[]);
void title(void);
void message(char msg[]);
int quit(void);
void init_costs(double *c, int t);
void show_costs(double *c, int t);
void getstring(char *desc);
void strcase(char *desc);
char *department[] = {"Camping", "Tennis", "Golf", "Snow Sports", "Water Sports"};
main() /* main function */
{
int selection, active = TRUE;
do
{
selection = menu();
switch (selection)
{
case 1: add(); break;
case 2:
case 3:
case 4:
case 5: active = quit(); break;
}
}
while(active);
return 0;
}
/*========================menu()=================================================*/
int menu()
{
int choice;
title();
/* display menu items */
printf("\t1 = Add a record\n");
printf("\t2 = Report\n");
printf("\t3 = Delete a record\n");
printf("\t4 = Change a record\n");
printf("\t5 = Quit\n\n");
/* display selection question and validate */
choice = getint(1,5,"choice");
return (choice);
}
/*=======================add()==================================================*/
void add()
{
int prodNum, prodType, prodQuant;
double prodCost, prodPrice;
char choice;
double costTotalByType[6]; /* define array to hold total costs by type (0 != category */
char desc[40];
/* vars to have values read in from text file */
int min = 0, max = 0;
FILE *fp; /* create file pointer */
init_costs(costTotalByType, 6); /* initialize cost array to zero */
fp = fopen("limits.txt", "r"); /* open file for read only */
if (fp == NULL)
{
printf("File does not exist");
}
do
{
title();
/* display data entry items and take input */
while (fscanf(fp, "%d%d", &min, &max) != EOF)
prodNum = getint(min,max,"Product Number");
prodType = getint(min,max,"Product Type");
getstring(desc);
prodQuant = getint(min,max,"Product Quantity");
prodCost = getreal(min,max,"Product Cost");
prodPrice = getreal(min,max,"Product Price");
fclose(fp);
costTotalByType[prodType] += prodCost;
show(prodNum, prodType, prodQuant, prodCost, prodPrice, desc);
show_costs(costTotalByType,6);
/* ask continue? */
printf("Continue (Y/N): ");
scanf("%c%*c", &choice);
choice = toupper(choice);
}
while (choice != 'N');
}
/*=====================getint()====================================================*/
int getint(int min, int max, char item[])
{
int n;
printf("Enter the %s: %d to %d: ", item, min, max);
scanf("%d%*c", &n);
while (n < min || n > max)
{
message("\nChoice not in range.\a");
printf("Enter the %s: %d to %d: ", item, min, max);
scanf("%d%*c", &n);
}
return(n);
}
/*=====================getreal()====================================================*/
double getreal(double min, double max, char item[])
{
double n;
printf("Enter the %s: %.2f to %.2f: ", item, min, max);
scanf("%lf%*c", &n);
while (n < min || n > max)
{
message("\nChoice not in range.\a");
printf("Enter the %s: %.2f to %.2f: ", item, min, max);
scanf("%lf%*c", &n);
}
return(n);
}
/*===================show()======================================================*/
void show(int prodNum, int prodType, int prodQuant, double prodCost, double prodPrice, char desc[])
{
printf("\nThe product number is --> %0.4d", prodNum);
printf("\nThe product type is ----> %d", prodType);
/* convert description to proper case structure */
strcase(desc);
printf("\nThe description is -----> %s", desc);
printf("\nThe quantity is --------> %d", prodQuant);
printf("\nThe cost is ------------> %.2f", prodCost);
printf("\nThe price is -----------> %.2f\n", prodPrice);
/* display totals */
printf("\n\nTotal product price ----> %.2f", total(prodQuant, prodPrice));
printf("\nTotal product cost -----> %.2f", total(prodQuant, prodCost));
printf("\nTotal product profit ---> %.2f\n\n", (total(prodQuant, prodPrice) - total(prodQuant, prodCost)));
}
/*=================total()========================================================*/
double total(int q, double c)
{
return(q*c);
}
/*=====================message()====================================================*/
void message(char msg[])
{
printf("%s\n", msg);
}
/*====================title()=====================================================*/
void title()
{
/* clear screen */
system("cls");
/* display title */
printf("%s\n\n", COMPANY);
}
/*====================quit()=====================================================*/
int quit()
{
system("cls");
message("\n\tGoodbye!\n");
return 0;
}
/*=====================init_costs()==============================================*/
void init_costs(double *c, int t)
{
int i;
for (i = 1; i < t; i++)
{
*(c+i) = 0;
}
}
/*====================show_costs()===============================================*/
void show_costs(double *c, int t)
{
int i;
double total = 0;
printf("\nProduct Cost by Type\n\n");
printf("Type Dept Cost\n");
printf("----------------------------------\n");
for (i = 1; i < t ; i++)
{
printf("%2d %-13s %9.2lf\n", i, *(department+i-1), *(c+i));
total += *(c+i);
}
printf("\nTotal%29.2lf\n\n", total);
}
/*====================getstring()===========================================*/
void getstring(char *desc)
{
printf("Enter the description: ");
gets(desc);
}
/*====================strcase()=============================================*/
void strcase(char *desc)
{
int i = 1;
/* convert first ch to upper */
*(desc) = toupper(*desc);
while (*(desc+i) != (int)NULL)
{
*(desc+i) = tolower(*(desc+i)); /* convert to lower */
if (*(desc+i) == ' ') /* convert ch after space to upper */
{
i++;
*(desc+i) = toupper(*(desc+i));
}
i++;
}
}
/*=========================================================================*/