Thread: Input Operations Problem

  1. #1
    bignood
    Join Date
    Sep 2005
    Posts
    33

    Input Operations Problem

    I have this program that calculates an electric bill. The problem is in my last function total_industrial I am not allowed to have input operations anywhere but in the main function. How can I get this program to work the same way without having this:

    Code:
    printf("Enter the KWH at peak hours > "); 
    scanf("%lf", &peak_hours);
    printf("Enter the KWH at off-peak hours > ");
    scanf("%lf", &off_peak);
    in the last function?


    Code:
    #include <stdio.h> /* printf and scanf definitions */
    #define RESIDENTIAL_RATE 6.00 /* Rate for residential use */
    #define RESIDENTIAL_KWH .052 /* Charge per KWH used for residential */
    #define COMMERCIAL_RATE 60.00 /* Rate for commercial use */
    #define COMMERCIAL_KWH .045 /* Charge per KWH used for commercial after 1000 */
    #define INDUSTRIAL_PEAK_RATE 76.00 /* Rate for industrial use at peak rate  */
    #define INDUSTRIAL_PEAK_KWH .065 /* Charge per KWH durign peak rate */
    #define INDUSTRIAL_OFF_RATE 40.00 /* Rate for industrial use at off-peak rate */
    #define INDUSTRIAL_OFF_KWH .028 /* Charge per KWH during off-peak rate */
    #define NOTHING 0 /* Cost for no charge */
    /* Function Prototypes */
    
    /* precondition : kw is >= 0
       postcondition : price of bill is returned for residential */
    
    double total_residential(double kw);
    
    /* precondition : kw is >= 0
       postcondition : price of bill is returned for commercial */
    
    double total_commercial(double kw);
    
    /* precondition : kw is >=0
       postcondition : price of bill is returned for industrial */
    
    double total_industrial(double kw);
    
    int main(void)
    {
    double kw; /* kilowatts entered by the user */
    int account; /*user's account number */
    char ans; /* type of use being residential, commercial, or industrial */
    
    printf("Enter your account number > ");
    scanf("%d", &account);
    printf("Enter R for residential, C for commercial, or I for industrial use > ");
    scanf(" %c", &ans); 
     
    printf("Enter the total number of kilowatt-hours used > ");
    scanf ("%lf", &kw);
     
    if (ans == 'R' || ans == 'r')
       printf("Account %d: Your total bill is $ %6.2f \n\n", account, total_residential(kw));
    else if(ans == 'C' || ans == 'c')
       printf("Account %d: Your total bill is $ %6.2f \n\n", account, total_commercial(kw));
    else if(ans == 'I' || ans == 'i')
       printf("Account %d: Your total bill is $ %6.2f \n\n", account, total_industrial(kw));
    else 
       printf("Invalid Use! Did not use R, C, or I. \n\n");
    
    return(0);
    }
    
    /* function definitions */
     
    double total_residential(double kw)
    {
    double residential; /* bill for residential */  
    if(kw > 0)
      residential = RESIDENTIAL_RATE + (kw * RESIDENTIAL_KWH);
    else
      residential = NOTHING;
    
    return(residential);
    }
    
    double total_commercial(double kw)
    {
    double additional_kwh; /* price for each KWH after 1000 */
    double commercial; /* bill for commercial use */
    additional_kwh = kw - 1000;
       
    if(kw < 1000 && kw > 0)
      commercial = COMMERCIAL_RATE;
    else if (kw <= 0)
      commercial = NOTHING;
    else
      commercial = COMMERCIAL_RATE + (additional_kwh * COMMERCIAL_KWH);
     
    return(commercial);
    }
     
    double total_industrial(double kw) 
    {
    double industrial; /* bill for industrial use */
    double additional_peak; /* price for each KWH at peak hours after 1000 */
    double additional_off; /* price for each KWH at off-peak hours after 1000 */
    double peak_hours; /* total KWH used at peak hours */
    double off_peak; /* total KWH used at off-peak hours */
    
    printf("Enter the KWH at peak hours > "); 
    scanf("%lf", &peak_hours);
    printf("Enter the KWH at off-peak hours > ");
    scanf("%lf", &off_peak);
     
    additional_peak = peak_hours - 1000;
    additional_off = off_peak - 1000;
    
    if(peak_hours > 1000 && off_peak > 1000)
      industrial = (INDUSTRIAL_PEAK_RATE + (additional_peak * INDUSTRIAL_PEAK_KWH)) + (INDUSTRIAL_OFF_RATE + (additional_off * INDUSTRIAL_OFF_KWH));
    else if(peak_hours < 1000 && peak_hours > 0 && off_peak > 1000)
      industrial = (INDUSTRIAL_PEAK_RATE + (INDUSTRIAL_OFF_RATE + (additional_off * INDUSTRIAL_OFF_KWH)));
    else if(peak_hours < 1000 && peak_hours > 0 && off_peak < 1000 && off_peak > 0)
      industrial = INDUSTRIAL_PEAK_RATE + INDUSTRIAL_OFF_RATE;
    else if(peak_hours > 1000 && off_peak < 1000 && off_peak > 0)
      industrial =((INDUSTRIAL_PEAK_RATE + (additional_peak * INDUSTRIAL_PEAK_KWH)) + INDUSTRIAL_OFF_RATE);
    else if(peak_hours == 0 && off_peak > 1000)
      industrial = INDUSTRIAL_OFF_RATE + (additional_off * INDUSTRIAL_OFF_KWH);
    else if(peak_hours > 1000 && off_peak == 0)
      industrial = (INDUSTRIAL_PEAK_RATE + (additional_peak * INDUSTRIAL_PEAK_KWH));
    else if(peak_hours == 0 && off_peak == 0)
      industrial = NOTHING;
    else if(peak_hours < 1000 && peak_hours > 0 && off_peak == 0)
      industrial = INDUSTRIAL_PEAK_RATE;
    else if(peak_hours == 0 && off_peak < 1000 && off_peak > 0)
      industrial = INDUSTRIAL_OFF_RATE;
    return(industrial);
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could ask for the input in your main() function and then pass the input to total_industrial() for it to calculate, rather than doing the actual input inside the total_industrial() function.
    If you understand what you're doing, you're not learning anything.

  3. #3
    bignood
    Join Date
    Sep 2005
    Posts
    33
    Another thing that I don't know how to do...

    In the first to functions I need to ask for KWH, but the third function I need to ask for two different things. How do I make it so if the user puts either residential or commercial it will ask for KWH, but if teh user puts industrial it will ask for the other two things instead?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    What you want to do is ask for the two "extra" inputs as soon as you know that the user is going to do an "industrial" use of the program. I see no better time than in the "I" clause of your makeshift switch statement.

    Although I haven't tested this, this should probably get you closer to what you want. I have marked the areas changed.

    Code:
    #include <stdio.h> /* printf and scanf definitions */
    #define RESIDENTIAL_RATE 6.00 /* Rate for residential use */
    #define RESIDENTIAL_KWH .052 /* Charge per KWH used for residential */
    #define COMMERCIAL_RATE 60.00 /* Rate for commercial use */
    #define COMMERCIAL_KWH .045 /* Charge per KWH used for commercial after 1000 */
    #define INDUSTRIAL_PEAK_RATE 76.00 /* Rate for industrial use at peak rate  */
    #define INDUSTRIAL_PEAK_KWH .065 /* Charge per KWH durign peak rate */
    #define INDUSTRIAL_OFF_RATE 40.00 /* Rate for industrial use at off-peak rate */
    #define INDUSTRIAL_OFF_KWH .028 /* Charge per KWH during off-peak rate */
    #define NOTHING 0 /* Cost for no charge */
    /* Function Prototypes */
    
    /* precondition : kw is >= 0
       postcondition : price of bill is returned for residential */
    
    double total_residential(double kw);
    
    /* precondition : kw is >= 0
       postcondition : price of bill is returned for commercial */
    
    double total_commercial(double kw);
    
    /* precondition : kw is >=0
       postcondition : price of bill is returned for industrial */
    
    double total_industrial(double kw,double I_peak_hours, double I_off_peak);  /**** changed to match edit  ***/
    
    int main(void)
    {
    double kw; /* kilowatts entered by the user */
    int account; /*user's account number */
    char ans; /* type of use being residential, commercial, or industrial */
    
    /****** Edited ******/
    double peak_hours; /* total KWH used at peak hours */
    double off_peak; /* total KWH used at off-peak hours */
    /******************/
    
    printf("Enter your account number > ");
    scanf("%d", &account);
    printf("Enter R for residential, C for commercial, or I for industrial use > ");
    scanf(" %c", &ans); 
     
    printf("Enter the total number of kilowatt-hours used > ");
    scanf ("%lf", &kw);
     
    if (ans == 'R' || ans == 'r')
       printf("Account %d: Your total bill is $ %6.2f \n\n", account, total_residential(kw));
    else if(ans == 'C' || ans == 'c')
       printf("Account %d: Your total bill is $ %6.2f \n\n", account, total_commercial(kw));
    else if(ans == 'I' || ans == 'i')
       /*******  Edited  *********/
       printf("Enter the KWH at peak hours > "); 
       scanf("%lf", &peak_hours);
       printf("Enter the KWH at off-peak hours > ");
       scanf("%lf", &off_peak);
       /***********************/
       printf("Account %d: Your total bill is $ %6.2f \n\n", account, total_industrial(kw,peak_hours,off_peak));
    else 
       printf("Invalid Use! Did not use R, C, or I. \n\n");
    
    return(0);
    }
    
    /* function definitions */
     
    double total_residential(double kw)
    {
    double residential; /* bill for residential */  
    if(kw > 0)
      residential = RESIDENTIAL_RATE + (kw * RESIDENTIAL_KWH);
    else
      residential = NOTHING;
    
    return(residential);
    }
    
    double total_commercial(double kw)
    {
    double additional_kwh; /* price for each KWH after 1000 */
    double commercial; /* bill for commercial use */
    additional_kwh = kw - 1000;
       
    if(kw < 1000 && kw > 0)
      commercial = COMMERCIAL_RATE;
    else if (kw <= 0)
      commercial = NOTHING;
    else
      commercial = COMMERCIAL_RATE + (additional_kwh * COMMERCIAL_KWH);
     
    return(commercial);
    }
     
    double total_industrial(double kw,double I_peak_hours, double I_off_peak)    /*** changed some variables in here *****/
    {
    double industrial; /* bill for industrial use */
    double additional_peak; /* price for each KWH at peak hours after 1000 */
    double additional_off; /* price for each KWH at off-peak hours after 1000 */
     
    additional_peak = I_peak_hours - 1000;
    additional_off = I_off_peak - 1000;
    
    if(peak_hours > 1000 && off_peak > 1000)
      industrial = (INDUSTRIAL_PEAK_RATE + (additional_peak * INDUSTRIAL_PEAK_KWH)) + (INDUSTRIAL_OFF_RATE + (additional_off * INDUSTRIAL_OFF_KWH));
    else if(I_peak_hours < 1000 && I_peak_hours > 0 && I_off_peak > 1000)
      industrial = (INDUSTRIAL_PEAK_RATE + (INDUSTRIAL_OFF_RATE + (additional_off * INDUSTRIAL_OFF_KWH)));
    else if(I_peak_hours < 1000 && I_peak_hours > 0 && I_off_peak < 1000 && I_off_peak > 0)
      industrial = INDUSTRIAL_PEAK_RATE + INDUSTRIAL_OFF_RATE;
    else if(I_peak_hours > 1000 && I_off_peak < 1000 && I_off_peak > 0)
      industrial =((INDUSTRIAL_PEAK_RATE + (additional_peak * INDUSTRIAL_PEAK_KWH)) + INDUSTRIAL_OFF_RATE);
    else if(I_peak_hours == 0 && I_off_peak > 1000)
      industrial = INDUSTRIAL_OFF_RATE + (additional_off * INDUSTRIAL_OFF_KWH);
    else if(I_peak_hours > 1000 && I_off_peak == 0)
      industrial = (INDUSTRIAL_PEAK_RATE + (additional_peak * INDUSTRIAL_PEAK_KWH));
    else if(I_peak_hours == 0 && I_off_peak == 0)
      industrial = NOTHING;
    else if(I_peak_hours < 1000 && I_peak_hours > 0 && I_off_peak == 0)
      industrial = INDUSTRIAL_PEAK_RATE;
    else if(I_peak_hours == 0 && I_off_peak < 1000 && I_off_peak > 0)
      industrial = INDUSTRIAL_OFF_RATE;
    return(industrial);
    }
    Hope this helps.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. problem : input and calculation working together
    By itay390 in forum C Programming
    Replies: 13
    Last Post: 07-30-2005, 12:32 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM