Help needed with code to calculate power use
Here is the question:
Write a program that will calculate and print bills for the city power company. The rates vary on whether the use is residential, commercial, or industrial. A code of R means residential use, a code of C means commercial use, and a code of I means industrial use. The rates are computed as follows:
R: €6.052 per kwh used
C: €60 for the first 1000 kwh and 4.5c for each additional kwh
I: rate depends on time of usage:
Peak hours: €76 for the first 1000 kwh and 6.5c for each additional kwh
Off-peak hours: €40 for the first 1000 kwh and 2.8c for each additional kwh
Your program should prompt the user to enter and account number, the use code, and the necessary consumption figures in whole numbers of kwh. Your program should print out a bill on the screen.
Code:
#include <stdio.h>
void main()
{
// commercial customer
const int COM_LIMIT = 1000;
const double COM_RATE = 0.045;
const double COM_FEE = 60;
// residential customer
const double RES_RATE = 6.052;
// industrial customer
const int IND_LIMIT = 1000;
const double IND_RATE_PEAK = .065;
const double IND_FEE_PEAK = 76;
const double IND_RATE_OFF = .028;
const double IND_FEE_OFF = 40;
int account, consumption=0, off_consumption=0, peak_consumption=0;
double bill=0.0;
char code;
printf("Please enter your account number: ");
scanf("%d", &account);
printf("\nPlease enter your use code (R, C or I): ");
scanf("%c", &code);
if (code=='r' || code=='R')
{
printf("\nPlease enter your consumption figures in whole numbers of kwh: ");
scanf("%d", &consumption);
bill = consumption*RES_RATE;
}
if (code=='c' || code=='C')
{
printf("\nPlease enter your consumption figures in whole numbers of kwh: ");
scanf("%d", &consumption);
if (consumption < COM_LIMIT)
bill = COM_FEE;
else
bill = (consumption*COM_RATE) + COM_FEE;
}
if (code=='i' || code=='I')
{
printf("\nPlease enter your off-peak consumption figures in whole numbers of kwh: ");
scanf("%d", &off_consumption);
if (off_consumption < IND_LIMIT)
bill = IND_FEE_OFF;
else if (off_consumption >= IND_LIMIT)
bill = (consumption*IND_RATE_PEAK) + IND_FEE_PEAK;
printf("\nPlease enter your peak consumption figures in whole numbers of kwh: ");
scanf("%d", &peak_consumption);
if (peak_consumption < IND_LIMIT)
bill = IND_FEE_PEAK;
else if (peak_consumption >= IND_LIMIT)
bill = consumption*IND_RATE_OFF + IND_FEE_OFF;
}
printf("\nAccount number: %d", account);
printf("\nCode: %c", code);
printf("\nConsumption: %d", consumption);
printf("\nBill: %.2f", bill);
}
Here is the output I am getting:
Please enter your account number: 1021
Please enter your use code (R, C or I):
Account number:1021
Code:
Consumption: 0
Bill:0.00
The code looks fine to me but when I enter an account number it jumps straight to the end without even asking for the code and therefore not entering the if statements.
Thanks