Thread: Getting the switch statement to work.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Getting the switch statement to work.

    I cannot get this switch to work for some reason. I run compile and enter the account number and when I try to enter an 'r' or 'R' it does not run the functions I have in place. I am taking an intro class and can usually figure this stuff out, but I am completely lost on this one. I used bold to highlight the switch area.

    Code:
    #include <stdio.h>
    
    #define PER_KWH_USED 0.052    /* residential kilowatt per hour used */
    #define ADD_KWH 0.045         /* commercial additional kilowatt per hour used */
    #define PEAK_KWH 0.065        /* industrial peak additional kilowatt per hour used */
    #define OFF_PEAK_KWH 0.028    /* industrial off-peak additional kilowatt per hour used */
    
    /* function prototypes */
    
    double comp_r_due(int res_kilowatt_hrs);
    double comp_c_due(int com_kilowatt_hrs);
    double comp_i_due(int peak_hrs, int off_peak_hrs);
     
    int
    main(void)
    {
      int acct_num;                /* input - account number           */
      char service;                   /* input - type of service          */
      int res_kilowatt_hrs;            /* input - total residential kilowatt hours      */
      int com_kilowatt_hrs;            /*input - total commercial kilowatt hours        */
      int peak_hrs;                /* input - industrial peak hours     */
      int off_peak_hrs;             /* input - industrial off peak hours */
      double r_due;               /* output - residential amount due     */
      double c_due;                 /* output - commercial amount due    */
      double i_due;                 /* output - industrial amount due    */
    
    
    
      printf("Enter the account number > ");
      scanf("&#37;d", &acct_num);
      printf("Enter the code R - Residential \n   C - Commercial \n               I - Industrial\n\n ");
      scanf("%c", &service);
    
    
    
      switch (service)
    {
        case 'R':
        case 'r':
         printf("Enter the kilowatt-hours > ");
         scanf("%d", &res_kilowatt_hrs);
         printf("\n\n\n");
         printf("Residential use");
         printf("Account Number          %d",acct_num);
         printf("kilowatt-hours          %d",res_kilowatt_hrs);
         printf("Amount due           $ %2.f",r_due);
         break;
      
        case 'C':
        case 'c':
         printf("Enter the kilowatt-hours > ");
         scanf("%d", &com_kilowatt_hrs);
         printf("\n\n\n");
         printf("Commercial use");
         printf("Account Number          %d",acct_num);
         printf("kilowatt-hours          %d",com_kilowatt_hrs);
         printf("Amount due           $ %2.f",c_due);
         break;
    
        case 'I':
        case 'i':
         printf("Enter the peak kilowatt-hours > ");
         scanf("%d", &peak_hrs);
         printf("Enter the off-peak kilowatt-hours > ");
         scanf("%d", &off_peak_hrs);
         printf("\n\n\n");
         printf("Industrial use");
         printf("Account Number             %d",acct_num);
         printf("Peak kilowatt-hours        %d",peak_hrs);
         printf("Off-peak kilowatt-hours    %d",off_peak_hrs);
         printf("Amount due              $ %.2f",i_due);
         break;
         
      
        
    }             /* switch  */
         
      return(0);
         
    }
    Last edited by mtymightymike; 10-15-2008 at 06:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  3. Error proofing my switch statement.
    By Shamino in forum C++ Programming
    Replies: 10
    Last Post: 01-04-2008, 04:38 PM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  5. switch statement / command line
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 12-20-2001, 04:21 PM