1. ## 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 */

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

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

3. 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. 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 */

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 */

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.