If the meter size is not valid then processing for that customer should stop

If the meter size is not valid then processing for that customer should stop. If the meter size is valid then your program should continue input, processing and output.

How do I stop processing for the customer that puts in an invalid meter size?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double MeterSize(double meterSize)
{
    double baseWaterCharge;
    
    if( meterSize != 0.625 && meterSize != 0.75 && meterSize != 1.0)
    {
        
    }
    else
    {
        if( meterSize == 0.625)
            baseWaterCharge = 3.61;
        else if( meterSize == 0.75)
            baseWaterCharge = 4.23;
        else if( meterSize == 1.0)
            baseWaterCharge = 6.14;
    }


    return baseWaterCharge;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
double MeterSize(double meterSize)
{
    double baseWaterCharge;
    
    if( meterSize != 0.625 && meterSize != 0.75 && meterSize != 1.0)
    {
        return 0;
    }
    else
    {
        if( meterSize == 0.625)
            baseWaterCharge = 3.61;
        else if( meterSize == 0.75)
            baseWaterCharge = 4.23;
        else if( meterSize == 1.0)
            baseWaterCharge = 6.14;
    }


    return baseWaterCharge;
}
int main() {
   double baseWaterCharge=MeterSize(0.625);
   if (baseWaterCharge==0) {
      return 0; 
   }
}
You can put the values in an interval; you can put an else like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
double MeterSize(double meterSize)
{
    double baseWaterCharge;
    
    if( meterSize != 0.625 && meterSize != 0.75 && meterSize != 1.0)
    {
        
    }
    else
    {
        if( meterSize == 0.625)
            baseWaterCharge = 3.61;
        else if( meterSize == 0.75)
            baseWaterCharge = 4.23;
        else if( meterSize == 1.0)
            baseWaterCharge = 6.14;
    }
    else
    {     
                // invalid value; stop the program
     }


    return baseWaterCharge;
}


I think you know that you can stop the program by using return 0;
@cosminNTG: that last else will never be executed. and his invalid values are everything that meterSize != 0.625 && meterSize != 0.75 && meterSize != 1.0 so your invalid value comment is at the wrong place
Also be careful about comparing floats, because of the way they are represented, comparison don't always work.

0.1 can't be represented exactly so this won't work:

1
2
3
4
float a = 0.1;

if (10 * a == 1.0)  //almost guaranteed to be false


To make comparisons correctly, you need to google EPSILON.
i believe that should work if you add an f to it.
1
2
3
float a = 0.1f;

if (10.f * a == 1.0f) 
@gelatine

what exactly are you doing in main? I don't understand it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
double MeterSize(double meterSize)
{
    double baseWaterCharge;
    
    if( meterSize != 0.625 && meterSize != 0.75 && meterSize != 1.0)
    {
        return 0;
    }
    else
    {
        if( meterSize == 0.625)
            baseWaterCharge = 3.61;
        else if( meterSize == 0.75)
            baseWaterCharge = 4.23;
        else if( meterSize == 1.0)
            baseWaterCharge = 6.14;
    }


    return baseWaterCharge;
}
int main() {
   double baseWaterCharge=MeterSize(0.625);
   if (baseWaterCharge==0) {
      return 0; 
   }
}
TheIdeasMan is right. Using float/doubles there are little values modifications due to optimizations. So you should compare equality in distance between a specific value.

Like:

1
2
3
4
float DistanceBetween(float First, float Second)
{
    return abs(First - Second);
}


And using it like:

if(DistanceBetween(baseWaterCharge,0.f) < 0.1f) // is baseWaterCharge's difference from 0 minor than 0.1? If it is, then you'll know baseWaterCharge is between -0.1 and 0.1.

EDIT: Also, if you don't know what does if (baseWaterCharge==0) { return 0; } do, then you should learn something more. Also this doesn't look much like a personal "job" but more like a homework or some course.
Last edited on
I know what it does. Just not sure why it needs to be there. And yes it is homework
Well, it is "needed" to be there so if the meter size is not valid the program will quit. I hope I don't need to tell much more - nothing against explaining - but it's quite a little concept - return (value); will end main.
Gotcha, I appreciate the explanation.
@gelatine

i believe that should work if you add an f to it.


The only thing the f does is to say that it is a float, so that doesn't fix the problem.

The value of a float goes like this:

FloatValue = DesiredValue plus or minus a very small value.

So float 0.1 equals something like 0.099999997. Multiplying it by 10.0 gives 0.99999997 which is not equal to 1.0.

To fix this you need an EPSILON value. EPSILON is the biggest number so that 1.0 + EPSILON is still equal to 1.0. The EPSILON needs to be scaled to fit the numbers you have, ie if your number is 1000.0 then you need to have MyEpsilon = 1000.0 * EPSILON. There are different EPSILON's for doubles and 32 bit and 64 bit.

To check whether something equals a value, you check that something is between value + EPSILON, and value - EPSILON.


This all happens because of the way floats are represented. They are represented as binary fractions, so 1.11 in binary is equal to 1.0 + 0.5 + 0.25. Doing this means that most numbers (up to a limit of significant figures) can be closely represented.

It isn't worthwhile to find out in advance which numbers can represented exactly, so one really has to do the comparisons above.

EssGeEich's method is a good one, probably easier than what I just explained.

I didn't know about EPSILON. Is it like a constant in somewhere like cfloat or cmath ?
@EssGeEich

Yes, I think it is in math.h .

Probably should google epsilon, lots of articles on errors & comparisons.

I find in my coding that if I am dealing with units of metres say, I am only interested in answers to the millimetre, so I have a distance tolerance of 0.0005. However, if I do testing with values to 0.0001, the rounding I expected is not working because the 0.0005 distance tolerance suffers from the same problems. So I still need the DBL_EPSILON ( I use doubles exclusively, and with 64 bit) I haven't tried your absolute method, maybe that will work better.

I have a vague notion that C++ has a whole lot of things that operate on epsilon values.
Topic archived. No new replies allowed.