Hello
I’ve been working on an assignment for class where we are supposed to calculate a water bill. I’ve come up with a solution that compiles with out any warnings and works just as expected, except for the math results. When multiplying my variable _rate by 0.55 it appears to multiply by 0.715. If I change this to 0.5 it looks like 0.65.
If anyone can explain what I've done wrong it would be much appreciated.
Code:
/* Assignment 2. Lab_2_Q1.c
Program Name: Water Bill
Description: This program prompts for user input of a single digit
and display text string with input.
Variables: _account = Costomer acount number
_old = Old water meter reading
_new = New Water meter reading
_use = The amount if water used between readings
_bill = The amount of the bill for water use
Includes: stdio.h
Constants: None
*/
#include <stdio.h> //Proccessor Directive
int main () //Main Function
{
long _account, _old, _new, _use, _rate; //Variables
float _bill;
printf("Enter the Costomers Account number for\nfor water bill:> ");
scanf("%li", &_account); //Prompt user for account number
printf("Enter the previous water meter reading:> "); //Enter old reading
scanf("%li", &_old);
printf("Enter the new meter reading:> "); //Enter new reading
scanf("%li", &_new);
_use = _new - _old; //calculate water used
_rate = _use / 1000; //calculate rate catagory
printf("_rate = %li\n", _rate);
printf("_use modulus 1000 = %li\n", (_use % 1000));
if (_use % 1000 != 0) //round up for part of 1000L
{
_rate =+ 1;
}
printf("_rate = %li\n", _rate);
if ( 0 <= _rate <= 25) //Customer has no penalty
_bill = 30 + (_rate * 0.55);
if ( 25 < _rate <= 50) //Customer has 10% penalty
_bill = 30 + (_rate * 0.55) * 1.1;
if (50 < _rate <= 75) //Costomer has 20% penalty
_bill = 30 + (_rate * 0.55) * 1.2;
if (75 < _rate <= 100) //Costomer has 30% penalty
_bill = 30 + (_rate * 0.55) * 1.3;
if (_rate > 100) //Costomer has 60% penalty
_bill = 30 + (_rate * 0.55) * 1.6;
if (_use < 0) //For som reason cutomer has
{ //got a negative use. This is an Error
printf("There has been some problem\nProgram terminating!\n");
system("pause"); //Pause program and wait for key stroke
return(1); //Return 1 for Error end terminate
}
//Print out costomers bill
printf("Cutomer %li used %liL and their bill is $%f.\n", _account, _use, _bill);
system("pause"); //Pause program and wait for key stroke
return(0); //Return 0 for successful completion
} //End program
/*
OUTPUT
Enter the Costomers Account number for
for water bill:> 12345
Enter the previous water meter reading:> 20000
Enter the new meter reading:> 20001
_rate = 0
_use modulus 1000 = 1
_rate = 1
Cutomer 12345 used 1L and their bill is $30.715000.
Press any key to continue . . .
*/