This is a school assignment, so I don't expect too much help. Unfortunately, the class is online and the teacher cannot lecture like one can in person, so it is difficult for me to understand certain concepts. In this case it is the use of pointers and multiple functions. My code compiles alright, but after inputting the information into the program, it crashes. I feel lost, hopefully someone can help me understand this better as I really do want to improve my coding. So I apologize if my code is horrendous:
#include <stdio.h>
int get_data(int *cust_num, int *kwh);
int calculate_charge(int *kwh, double *rate);
int print_results(int *cust_num, int *kwh, double *rate);
int
main(){
int pwr, tot_cust, tot_kwh, customer;
char ans;
double charge, tot_charge;
do{
/*get data*/
get_data(&customer, &pwr);
/*calculate data*/
calculate_charge(&pwr, &charge);
/*print data*/
print_results(&customer, &pwr, &charge);
/*continue?*/
printf("\nDo you have any more data to input? (y/n)> ");
scanf("%c", ans);
tot_cust ++;
tot_kwh = tot_kwh + pwr;
tot_charge = tot_charge + charge;
}while(ans == 'y');
/*print final*/
printf("\nTotal Customers: %d Total KWH used: %d Total Charges:
%.2f", tot_cust, tot_kwh, tot_charge);
return 0;
}
int get_data(int *cust_num, int *kwh){
*cust_num = *cust_num;
*kwh = *kwh;
printf("Please enter the customer number and the kwh> ");
scanf("%d", &cust_num);
scanf("%d", &kwh);
}
int calculate_charge(int *kwh, double *rate){
/*calculate cost*/
if(*kwh <= 300){
*rate = .09 * *kwh;
}
else if ((*kwh > 300) && (*kwh <= 600)){
*rate = .09 * 300 + ((*kwh - 300) * .08);
}
else if ((*kwh > 600) && (*kwh <= 1000)){
*rate = .09 * 300 + .08 * 300 + ((*kwh - 600) * .06);
}
else {
*rate = .09 * 300 + .08 * 300 + .06 * 400 + ((*kwh - 1000) * .05);
}
}
int print_results(int *cust_num, int *kwh, double *rate){
printf("\nTotal Customers: %5d, Total kwh: %5d, Total charge: %5.2f",
*cust_num, *kwh, *rate);
}
scanffunction's requirements as addressed in Sourav's answer is one example of what I mean. That said, it was a decent question other than that, and I encourage you to ask more like it if you experience such difficulty in the future.