# Discussionneed help with C programming issue

Status
Not open for further replies.

#### holysprings

Write a program to calculate the water rates according to the table below:
An example of the output interface :

This program calculates the monthly water bill for you.
Please key in the water consumption for this month (in m^3): -10
Wrong! Can’t be negative value! Key in again!
Please key in the water consumption for this month (in m^3): 10
You need to pay for RM 2.50 for this month
Thanks for using my program.
Press any key to continue...

 Minimum Charge RM2.50 per month First 20,000 litres RM0.22 per 1,000 litres More than 20,000 litres to 40,000 litres RM0.46 per 1,000 litres More than 40,000 litres to 60,000 litres RM0.68 per 1,000 litres More than 60,000 litres to 200,000 litres RM1.17 per 1,000 litres More than 200,000 litres RM1.30 per 1,000 litres

#include <stdio.h>

float cur_meter, min_charge1=2.50,total1,total2,total3,total4,total5;

int main()
{
printf("This program calculates the monthly water bill for you.\n");
printf("Please key in the water consumption for this month (in m^3) \n");
scanf("%f", &cur_meter);

if(cur_meter==0){
printf("You need to pay for RM%f for this month\n", min_charge1); /no issue for displaying this/
}

if(cur_meter>0 && cur_meter<=20000){ /the output starts to get weird starting here total1 until total5/
total1=(cur_meter/1000)*0.22;
printf(" You need to pay for RM%f for this month\n", total1);

}
else if(cur_meter>20000 && cur_meter<=40000){
total2=total1+((cur_meter-20000)*0.46);
printf(" You need to pay for RM%f for this month\n", total2);

}
else if(cur_meter>40000 && cur_meter<=60000){
total3=total1+total2+((cur_meter-40000)*0.68);
printf(" You need to pay for RM%f for this month\n", total3);

}
else if(cur_meter>60000 && cur_meter<=200000){
total4=total1+total2+total3+((cur_meter-60000)*1.17);
printf(" You need to pay for RM%f for this month\n", total4);

}
else if(cur_meter>200000){
total5=total1+total2+total3+total4+((cur_meter-200000)*1.30);
printf(" You need to pay for RM%f for this month\n", total5);

}

while(cur_meter<0){ /no issue for displaying this as well/
printf("Wrong! Can’t be negative value! Key in again! \n");
printf("Please key in the water consumption for this month (in m^3) \n");
scanf("%f", &cur_meter);
}
}

The outputs are big ridiculous number -.-.... can someone kindly help me to look at this. C programming

#### COLGeek

##### Cybernaut
Moderator
Homework or class assignment?

#### rgd1101

##### Don't
Moderator
school don't teach debug anymore?

#### holysprings

school don't teach debug anymore?
I'm taking part time course, more on reading with slides and own pratices instead of face to face class. now I'm using online C compiler to do the programming. the program doesn't show error but the outputs are ...weird.

#### holysprings

it's ok if there nobody familiar with it . since C is not a commonly used language unlike JAVA

#### rgd1101

##### Don't
Moderator
there debug on the site. and display expression.
You can check the number.

Status
Not open for further replies.

Replies
2
Views
2K
Replies
3
Views
2K
Replies
4
Views
44K
Replies
3
Views
2K
Replies
4
Views
3K