``````//function example
#include <iostream>
#include <cmath>
using namespace std;

float cutoff(float& windchill)
{

int z;
z = (int)(windchill + 0.5);
return(z);

}

float calculate (int& t, int& v)
{

float windchill;
windchill = 35.74 + (0.6215 * t) - 35.75 * pow(v,0.16) + 0.4275 * t * pow(v,0.16);
return(windchill);

}

void input (int& temp, int& windspeed)
{

cout << "Enter the temperature: ";
cin >> temp;
cout << "\nEnter the windspeed: ";
cin >> windspeed;

}

int main ()
{

int temp, windspeed, z;
float windchill;

input(temp, windspeed);
cout << "\nTemp is: " << temp << "\nWindspeed is: " << windspeed;
calculate(temp,windspeed);
cout << "\n\nWindchill is: " << cutoff(windchill) << endl << endl;

return 0;
}``````

I need some help figuring out why when 'windchill' is returned to main Im getting the value "-1.07374e+008". When I want to just add 0.5 and truncate the decimal. Its doing this for anything I enter

Thanks,
Jordan

Edit: yes, I realize I spelled rounding wrong :p

Edited by JHus00: n/a

4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by HealBrains

I would suggest changing all of your floating point variables and returns from float to double. There is a difference in how floats and doubles are handled in the system and there is a difference in precision. You may be getting some inaccuracies because of the mingling of the 2 types. There are 2 reasons:
1. The function pow() accepts and returns double values.
2. The compiler assumes that you have entered a double if you use a decimal point, unless you put an 'f' suffix on your number.

``````float fMyDecimal = 0.0;

fMyDecimal = 15.2;
//this is assigning a double to a float, there could be errors
//the compiler is also likely to at least return a warning about truncation

fMyDecimal = 15.2f;  //notice the 'f'
//this is assigning a float, there should be no data loss or errors``````

Edited by Fbody: n/a

Now it's time to do duck-debugging!

Explain what this line

``calculate(temp,windspeed);``

and this function

``````float calculate (int& t, int& v)
{

float windchill;
windchill = 35.74 + (0.6215 * t) - 35.75 * pow(v,0.16) + 0.4275 * t * pow(v,0.16);
return(windchill);

}``````

do.

It looks like you're using an uninitialised variable...

You never use the float 'windchill' until you add 0.5 to it. So basically you're adding 0.5 to some random number.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.