How to display something before terminating the program

This is a discussion on How to display something before terminating the program within the C Programming forums, part of the General Programming Boards category; Hi all! I am very closed to finished with this particular program but I want to be able to display ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    How to display something before terminating the program

    Hi all!

    I am very closed to finished with this particular program but I want to be able to display my showCost() function before terminating the entire program when the user is prompted
    "Continue (y/n): ?" I know it has something to do with the quit() function and the loop but I am lost here.

    Here is my code

    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. .

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Add an else-if block to your quit() that is processed when the user enters 'N'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. My Program Won't Display First Few Data...
    By nino613 in forum C Programming
    Replies: 2
    Last Post: 12-08-2009, 01:50 PM
  3. A C++ program problem.
    By hector_ronado in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2009, 09:26 AM
  4. Program Display
    By Axiom in forum C Programming
    Replies: 3
    Last Post: 11-08-2008, 04:48 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21