I'm trying to write a program that uses a function to calculate a water bill. It reads the information about water usage from a file, then calculates the water bill after tax.
This is the file:
g 5000
B 1250
M 50
This is what the output should be:
Type Water usage Cost including tax($)
g 5000 194.30
B 1250 93.89
Wrong user type
This is my output:
Type Water usage Cost including taxWrong user type.
g 5000 0.000000
B 1250 18.750750
Wrong user type.
I'm not sure if the problem lies in my formula or something else. There's obviously a problem with the if else statement in the function since it keeps printing "Wrong user type" where it shouldn't.
This is my code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
double water_billcalculation(char user_type, double water_use, double bft, double at);
int main(void) {
FILE* water;
water = fopen("water_usage.txt", "r");
char user_type;
int cf;
double bft = 0, at = 0, water_use = 0;
printf("Type Water usage Cost including tax");
while (fscanf(water, "%c%d ", &user_type, &cf) != EOF) {
//water_billcalculation(user_type, water_use, bft, at);
printf("%c %d %lf\n", user_type, cf, water_billcalculation(user_type, water_use, bft, at));
}
return(0);
}
double water_billcalculation(char user_type, double water_use, double bft, double at) {
if (user_type == 'G') {
bft = (water_use * 0.035) + 3.75;
at = bft + (bft * .087);
}
else if (user_type == 'B') {
bft = (water_use * .0553) + 17.25;
at = bft + (bft * .087);
}
else if (user_type == 'R') {
if (water_use <= 400) {
bft = (water_use * .04) + 13.5;
at = bft + (bft * .087);
}
else if (water_use > 400 && water_use <= 700) {
bft = (water_use * .062) + 13.5;
at = bft + (bft * .087);
}
else {
bft = (water_use * .12) + 13.5;
at = bft + (bft * .087);
}
}
else {
printf("Wrong user type.\n");
}
return(at);
}