School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

C Source Code



Switch case

a simple program that demonstrates the use of the switch case

Submitted By: browngod2002
Actions:
Rating:
Views: 5,433

Language: C

Last Modified: October 4, 2006
Instructions: select a pay code the enter the information and the program will calculate the pay for the week.

Snippet


  1.                                   /* Matthew Schofield */
  2.                    /*       C           */
  3. /**********************************************************/
  4. /* Matthew Schofield                                      */
  5. /* 9-28-2006                                              */
  6. /*                                                        */
  7. /* This program will use the switch functon to process    */
  8. /* input from the user.                                   */
  9. /* Input: get the pay rate,hours,sales,or pieces made     */
  10. /* Output: The pay for the week for each employee         */
  11. /*          formatted to two decimal places.              */
  12. /**********************************************************/
  13.  
  14. #include "stdafx.h"
  15. #include <stdio.h>
  16. /* The define symbolic constants */
  17. #define PERCENT .057
  18. #define OVERTIME 1.5
  19. #define COMMISSION 250
  20.  
  21. /* function prototype */
  22. float managerpay(float salary);
  23. float hourlypay(int hours, float pay);
  24. float commpay(int sales);
  25. float pwpay(float wage, int pieces);
  26.  
  27.  
  28. int _tmain(void)
  29. {
  30.  
  31.      int paycode;
  32.      float salary;
  33.      float wage;
  34.      int pieces;
  35.      float pay;
  36.      int sales;
  37.      int hours;
  38.      
  39.  
  40.  
  41.      printf("Enter the employee's pay code to calculate salary\n"
  42.           "1 = Manager\n"
  43.           "2 = Hourly Worker\n"
  44.           "3 = Commission Worker\n"
  45.           "4 = Piece Worker\n"
  46.           "5 to exit: ");
  47.           scanf("%d", (int *)&paycode);
  48.                 printf("\n");
  49.  
  50.      
  51. /* The switch used to get the inputs, call the functions to calculate the pay
  52.      for each paycode and display the results formatted to 2 decimal places */
  53.  
  54.      while(paycode != 5) /*start of while loop */
  55.      {
  56.           switch (paycode)/*start of switch*/
  57.           {
  58.           case 1:
  59.                printf("Enter the managers salary: ");
  60.                scanf("%f", &salary);
  61.            
  62.                         printf("The managers pay is: $%.02f\n", managerpay(salary));
  63.                
  64.                break;
  65.  
  66.           case 2:
  67.                         printf("Enter the hours worked: ");
  68.                scanf("%d", &hours);
  69.  
  70.                printf("Enter the salary: ");
  71.                scanf("%f", &pay);
  72.  
  73.                  printf("The employee's pay is: $%.02f\n",float (hourlypay(hours,pay)));
  74.            
  75.                break;
  76.  
  77.           case 3:
  78.                         printf("Enter the sales: ");
  79.                scanf("%d", &sales);
  80.  
  81.                 printf("The employee's pay is: $%.02f\n", commpay(sales));
  82.            
  83.                break;
  84.  
  85.           case 4:
  86.                         printf("Enter the number of pieces made: ");
  87.                scanf("%d", &pieces);
  88.  
  89.                printf("Enter the wage: ");
  90.                scanf("%f", &wage);
  91.  
  92.                printf("The employee's pay is: $%.02f\n", pwpay(wage,pieces));
  93.            
  94.                break;
  95.  
  96.          case 5:
  97.             getchar();
  98.             getchar();
  99.  
  100.             return 0;
  101.  
  102.             break;
  103.            
  104.  
  105.         default:
  106.             printf("You didn't enter the correct pay code.\n");
  107.                
  108.           } /*end of switch*/
  109.  
  110.             printf("\nEnter the employee's pay code to calculate salary\n"
  111.                     "1 = Manager\n"
  112.                     "2 = Houlry Worker\n"
  113.                     "3 = Commission Worker\n"
  114.                     "4 = Piece Worker\n"
  115.                     "5 to exit: ");               
  116.             scanf("%d", (int *)&paycode);
  117.             printf("\n");
  118.  
  119.      }       /*end of while loop*/
  120.      
  121.         getchar();
  122.         getchar();
  123.  
  124.           return 0;
  125. }
  126.  
  127. /* managerpay() : Takes in the salary for a manager for a year and
  128.                   returns the weekly salary.
  129.       Input: salary as a float.
  130.        Output: the salary for a week as a float */
  131.  
  132.  
  133. float managerpay(float salary)
  134. {
  135.      
  136.  
  137.      return salary / 52;
  138. }
  139.  
  140.  
  141. /* hourlypay () : takes in the hours and pay for an hourly employee
  142.                   and returns the weekly salary. If overtime has been
  143.                       worked for the week then it will calculate the overtime
  144.                       and add it to the weekly pay. If the hours are less than
  145.                       40 the weekly pay will still be calculated.
  146.      Input: hours as an int and pay as a float
  147.       Output: the weekly pay as a float*/
  148.  
  149.  
  150. float hourlypay(int hours, float pay)
  151. {
  152.      float pay2;
  153.  
  154.      if (hours <= 40)     
  155.           pay2 = float(hours * pay);
  156.      else       
  157.           pay2 = float((pay * OVERTIME) * (hours - 40) + (pay * 40));
  158.      
  159.      return pay2;
  160. }
  161.  
  162.  
  163. /* commpay() : takes in the sales and the commission for the commission worker
  164.                and retuns their pay for the week.
  165.     Input: commission as a float and sales as an int
  166.      Output: the weekly pay for the worker as a float */
  167.  
  168.  
  169. float commpay(int sales)
  170. {
  171.      float pay;
  172.      
  173.  
  174.      pay =COMMISSION + PERCENT * sales;
  175.  
  176.      return pay;
  177. }
  178.  
  179.  
  180. /*pwpay () : takes in the number of pieces made for the week
  181.              and the pay that the piece worker gets for each
  182.                 piece made.
  183.     Input: wage as a float and pieces as an int
  184.      Output: the weekly pay as a float */
  185.  
  186.  
  187. float pwpay(float wage, int pieces)
  188. {
  189.      float pay;
  190.  
  191.      pay = wage * pieces;
  192.  
  193.      return pay;
  194. }

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.