Thread: Help needed with code to calculate power use

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    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.
    Last edited by dave_the_bear10; 10-10-2011 at 08:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-23-2011, 01:23 AM
  2. Trouble understanding code to calculate sum of n numbers
    By jackloring in forum C Programming
    Replies: 8
    Last Post: 11-13-2010, 04:33 AM
  3. power calculate
    By isthanblue in forum C Programming
    Replies: 11
    Last Post: 12-09-2007, 02:14 PM
  4. how can we calculate excution time and memory needed
    By elton_fan in forum C Programming
    Replies: 3
    Last Post: 09-25-2007, 08:09 AM
  5. what does this code calculate?
    By Guti14 in forum C++ Programming
    Replies: 4
    Last Post: 09-17-2003, 07:39 PM

Tags for this Thread