0

So I am very new to programming. I'm trying to create a program for a class assignment that calculates the Reynolds number of liquid flowing through a pipe. The assignment requires me to use "if" statements to determine the actual viscosity of the liquid based on the temperature that the user enters when prompted. However, only the last "if" statement calculates a correct value. All of the "if" statements have the same structure, but only the last one works. Please help.

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;


int main()
{
    double Rnumber, Velocity, viscosity, diameter, temp;


    cout << "Enter the temperature of the liquid (degrees Celsuis): " << endl;
    cin >> temp;

    if (temp == 5.0)
    {
        viscosity = (1.49 * pow(10, -6));
    }

    if (temp == 10.0)
    {
        viscosity = (1.31 * pow(10, -6));
    }

    if (temp == 15.0)
    {
        viscosity = (1.15 * pow(10, -6));
    }

    cout << "Enter the velocity of the liquid (m/s): " << endl;
    cin >> Velocity;
    cout << "Enter the diameter of the pipe (m): " << endl;
    cin >> diameter;

    Rnumber = ((Velocity * diameter) / (viscosity));

    cout << "The Reynolds number for the system is " << Rnumber << " ."<< endl;

    cin.ignore(2);
    return 0;
}
  • 1
    What input do you give your program when running? What output did you expect to see? What output did you actually get? And perhaps it's time for you to [learn how to debug your program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? – Some programmer dude Jun 19 '18 at 21:35
  • Also, think a little about what happens if the input is e.g. *between* `10.0` and `15.0`? Or *above* `15.0`? Or *below* `5.0`? – Some programmer dude Jun 19 '18 at 21:36
  • Have you tried using else if statements? – Sailanarmo Jun 19 '18 at 21:38
  • 3
    Using `==` with floating point is dodgy, see [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – M.M Jun 19 '18 at 21:48

2 Answers2

1

you should not compare floats for equality .

in your case this might do the trick for the comparison:

bool floatEqual(double a, double b)
{
    const double epsilon = 0.001;
    return ((a + epsilon) > b) && ((a - epsilon) < b);
}

but in general this is not a good approach (see http://floating-point-gui.de/errors/comparison/)

also consider to handle cases outside your desired tempeature range (at least do some error handling)

your program should handle any given value, e.g. 5.1 degrees. you might want to attach your viscosities to range instead of precise points.

e.g. something like this will also avoid the equality problem:

if (temp < 7.5)
{
    viscosity = (1.49 * pow(10, -6));
}
else if (temp < 12.5)
{
    viscosity = (1.31 * pow(10, -6));
}
else
{
    viscosity = (1.15 * pow(10, -6));
}
skeller
  • 1,151
  • 6
  • 6
0

If you only check for those specific values, you could make temp an int and compare like so: if(temp == 5). If its all .0 values anyways.

You could also considder a switch statement so you dont need all those if statements

switch (temp)
{
case 5:
    //....
    break;
case 10:
    //.... etc.
    break;  
default:
   //....
    break;
}
Tim L.
  • 49
  • 1
  • 6