User Defined Function....

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

Sorry I suck im new lol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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;
	    
	    else if(Kwu <= 300) 
	    	Tot = (Kwu * 0.09);
	 
		else if(Kwu <= 600)
	    	Tot = ((Kwu - 300) * 0.08) + 27;
	    
		else if(Kwu <= 1000)
	    	Tot = ((Kwu - 600)* 0.06) + 51;
		
		else if(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);
	
}
Topic archived. No new replies allowed.