I have this code working but I want to try using a defined function, how would I go about doing that? I am trying to create a function that will calculate the customer charges and return the charges. I think I need pointers too as well to display the final total charges, KWH used, etc.
Also, when I exit the program using -1 -1, it subtracts 1 from the total KWH.... So if the total KWH used is 725 it will instead be 724...
#include <stdio.h>
int
main(void)
{
int Cust = 0;
int Kwu = 0;
double Tot = 0;
int TotCust = 0;
int KwuTot = 0;
double ChargeTot = 0;
do /* main charge calculation functions */
{
printf("Enter customer number and kwh (-1 to quit): ");
scanf("%ld %ld", &Cust, &Kwu);
if(Cust != -1)
TotCust++; /* Stores each customer number input as one and keeps adding each additional input by 1. */
KwuTot = KwuTot + Kwu; /* Adds KWH totals for use at end */
if (Cust == -1)
break;
elseif(Kwu <= 300)
Tot = (Kwu * 0.09);
elseif(Kwu <= 600)
Tot = ((Kwu - 300) * 0.08) + 27;
elseif(Kwu <= 1000)
Tot = ((Kwu - 600)* 0.06) + 51;
elseif(Kwu > 1000)
Tot = ((Kwu - 1000) * 0.05) + 75;
ChargeTot = ChargeTot + Tot; /* Adds total charges for use at end */
printf("Customer Num: %3d KWH Used: %4d Charge: %7.2f\n", Cust, Kwu, Tot); /*prints column headings for output */
} while (Cust != -1);
printf("\nTotal Customers: %2d Total KWH Used: %5d Total Charges: %8.2f\n", TotCust, KwuTot, ChargeTot); /*prints final column headings once program is done */
return(0);
}