Getting the switch statement to work.

This is a discussion on Getting the switch statement to work. within the C Programming forums, part of the General Programming Boards category; I cannot get this switch to work for some reason. I run compile and enter the account number and when ...

  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 07:05 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Had you printed out service in some kind of debugging attempt, you would have discovered that it was not R. Or C. Or I. But rather \n. After all, enter-key is a perfectly good character. As we told the last person who asked this exact question, use " %c" to make the scanf ignore the non-printing characters.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not saying this is the issue, but as a suggestion, why not change services to an array.

    Code:
    char services[2];
    And have scanf() read a string of a length of 1 digit.

    Code:
    scanf("&#37;1s", services);
    This will take care of the user typing in any pesky leading whitespace.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    I am still lost on this. Where would I put in that &#37;c?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mtymightymike View Post
    I am still lost on this. Where would I put in that %c?
    You bolded it, I would expect you know where it is. (In other words, this is the same %c that you already have. Note however that the format string is " %c", not "%c".)

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    When bold fails......

    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);
         
    }

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    ok I understand now. Thanks very much.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    See, when in doubt make things huge

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, 02: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

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