Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define COMPANY "Sierra Sporting Goods"
#define MINN 1
#define MAXN 9999
#define MAXQ 50
#define MINQ 1
#define MINT 1
#define MAXT 5
#define MINC 5
#define MAXC 900
#define MINP 6
#define MAXP 1000
#define TRUE 1
#define FALSE 0
int menu(void);
int quit(void);
int prompt(char msg[]);
void titles(void);
void message(char msg[]);
int getint(int min, int max, char item[]);
void add(void);
double getreal(double min, double max, char item[]);
void show(int n, int numb, int qty, int type, double cost, double price, double tc, double tp);
double total(double amount, int qty);
void initCost(double t[], int c);
void showCost(double t[], int c);
main()
{
int key, more = TRUE;
do{
key = menu();
switch (key)
{
case 1: add();break;
case 2:
case 3:
case 4:
case 5: more = quit(); break;
default: message("\nError in selection\a");
//displays error message if 2,3,4 is selected
//calls the quit() function if 5 is selected
}
}
while (more);
return 0;
}
int menu()
{
int choice;
titles(); //displays company name
/*choices to select*/
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 = getint(1,5 ,"Enter your selection: ");//enter min 1 or max 5
return(choice);
}
void titles(void)
{
system("cls");
printf("%s\n\n", COMPANY); /*will display company name in menu*/
/*function when titles is called*/
}
int quit()
{
int x, more = TRUE;
x = prompt("\nExit Program?"); //if 5 is selected on menu
if (x == 'Y')
{
system("cls");
message("\nProgram terminated");
more = FALSE;
}
return more;
}
void initCost(double t[], int c)
{
int i;
for(i = 1;i<c;i++)
t[i] = 0;
}
void showCost(double t[], int c)
{
int i;
double total = 0;
printf("Type Cost\n\n");
for (i = 1;i<c;i++)
{
printf("%2d%3.2lf\n", i, t[i]);
total += t[i];
}
printf("%3.2lf\n", total);
}
void add() {
int n=0, numb, qty, type, choice;
double cost, price, tc, tp;
double types[5];
initCost(types, 5);
do
{
system("cls");
titles(); //print company name
numb = getint(MINN, MAXN, "product number");
qty = getint(MINQ, MAXQ, "product quantity");
type = getint(MINT, MAXT, "product type");
cost = getreal(MINC, MAXC, "product cost");
price = getreal(MINP, MAXP, "product price");
tc = total(cost, qty); //call the pricetotal() function
tp = total(price, qty); //call the costtotal()function
show(++n, numb, qty, type, cost, price, tc, tp); //call show() function
types[type] += cost;
choice = prompt("Continue");
}
while (choice =='Y');
showCost(types, 5);
getchar();
}
//out of the loop, all user input has been validated
void show(int n, int numb, int qty, int type, double cost, double price, double tc, double tp)
{
//this is everything that will be displayed once information is entered
titles(); //display company name
printf("\nThe product number is %04d", numb); /*print out input*/
printf("\nThe product type is %01d", type);
printf("\nThe quantity is %03d", qty);
printf("\nThe cost is %3.2lf", cost);
printf("\nThe price is %3.2lf", price);
printf("\n\n");
printf("\nThe total cost is %3.2lf", tc);
printf("\nThe total price is %3.2lf", tp);
printf("\nPress enter to continue");
getchar();
}
int getint(int min, int max, char item[])
{
int n;
printf("\nEnter the %s from %d to %d:\a", item, min, max);
scanf("%d*c*",&n);
while (n < min || n > max)
{
message("\nError, please enter a number within range:\a");
printf("\nEnter the %s from %d to %d:\a", item, min, max);
scanf("%d*c*",&n);
}
return (n);
}
double getreal(double min, double max, char item[])
{
double n;
printf("\nEnter the %s from %3.2lf to %3.2f:\a", item, min, max);
scanf("%lf%*c",&n);
while (n < min || n > max)
{
message("\nError, please enter a number within range\a");
printf("\nEnter the %s from %3.2lf to %3.2lf\a", item, min, max);
scanf("%lf%*c",&n);
}
return (n);
}
//--------------------------------------------//
double total(double amount, int qty)
{
return (amount * qty);
}
// ----------------------------------------//
void message(char msg[])
{
printf("%s, press Enter key to continue\n", msg);
getchar();
}
int prompt(char msg[])
{
int ch;
do
{
printf("%s(Y/N?)",msg);
ch = toupper(getchar());
if (ch != 'Y' && ch !='N')
printf("\nError, try Y or N only\n");
}
while (ch != 'Y' && ch!='N');
return(ch);
}
I want it so when the user enters 'N' it spits out the showCost() function before terminating the program but I do not know how to go on about doing this. Any suggestions? Any help would be greatly appreciated thanks. .