0

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);
}
6
  • 1
    Do you have the log / error message / dump information? Commented Feb 23, 2015 at 19:18
  • run the code under a debugger (you don't say what platform you are on) this will give more info about where its going wrong Commented Feb 23, 2015 at 19:28
  • I am using Dev C++ . Commented Feb 23, 2015 at 23:17
  • After working on this and playing around with pointers in function in a side project I came to a workable solution. Because this is a school assignment, I do not want to put the complete code on here just because I do not want someone who might be in my class to have access to a complete code. Commented Feb 24, 2015 at 19:34
  • I would suggest in the future to express how would you like it to behave as well as how it currently behaves. While we can likely deduce the current behavior from the code, we are unaware of your understanding of some concepts that would be necessary here. Your misunderstanding of the scanf function'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. Commented Feb 24, 2015 at 23:10

1 Answer 1

3

There are many issues in your code. To start with,

Point 1. scanf("%c", ans); should be scanf(" %c", &ans);

Point 2. you're doing tot_cust ++;, without initializing tot_cust.

Point 3. in get_data() function, cust_num and kwh are pointers. You don't need to put & while using in scanf().

Point 4. *cust_num = *cust_num; and *kwh = *kwh; statements are not required.

and maybe many more. As suggested by Mr. @WhozCraig, please enable compiler warnings and try to fix them.

Sign up to request clarification or add additional context in comments.

2 Comments

Note: Most of the issues in this code are identifiable if warnings are turned up on the compiler to pedantic levels.
@WhozCraig Absolutely. Updated my answer to include the suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.