Thread: Unexpected results

  1. #1
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12

    Unexpected results

    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 . . .
       
    */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For one, get rid of the underscore on all of your variable names to avoid name space issues. Next, all of your if checks are wrong. You can't do:
    Code:
    if( a < b < c )
    and expect it to work right. You have to do this:
    Code:
    if( a < b && b < c )

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12
    Thanks
    While Im not sure what name space is, I've changed the varible names.
    Changing the check has done the job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Unexpected Results
    By Bailz in forum C Programming
    Replies: 5
    Last Post: 11-08-2006, 07:10 AM
  3. Unexpected results from function
    By ajdspud in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2005, 04:19 PM
  4. Unexpected results using argc / *argv[]
    By Morgan in forum C Programming
    Replies: 1
    Last Post: 09-11-2003, 01:38 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM