# Thread: Help needed with code to calculate power use

1. ## 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;

scanf("%d", &account);

scanf("%c", &code);

if (code=='r' || code=='R')
{
scanf("%d", &consumption);
bill = consumption*RES_RATE;
}

if (code=='c' || code=='C')
{
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:

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

2. > printf("\nPlease enter your use code (R, C or I): ");
> scanf("%c", &code);
%c always reads the next character - which in your case would be the newline from the previous conversion.

For all the other common conversions, initial whitespace skipping is automatic.
To force it to happen for %c, write
scanf(" %c", &code);

Oh, and main returns int, not void (see the FAQ).

3. Works perfectly now thanks

Now I need to redirect the output to a text file?

I've looked at a few examples but I'm not really understanding it?

FILE *file = fopen("file.txt", "w");

Think that's what I need but I don't know where to put it or how to use it.