Another new coder here, and yes, I am a student. I am NOT asking for a solution, but I am asking for guidance to point me in the right direction. I am supposed to write a code that receives input from the terminal as to the temperature in Fahrenheit and the wind speed in meters per second. Then use 3 sub functions to 1) convert Fahrenheit to Celsius, 2) calculate windchill, and 3) convert from Celsius back to Fahrenheit. Then output the results if the temperature in C is less than or equal to 10 degrees. I have written the code and added debugging comments to show the value of the variables throughout the calculations and no matter what is inputted, the degrees C (variable "tempC") always comes out to zero. Can someone help me find my error with the code below and point me in the right direction to correcting this part. I'm in hopes that I can debug the rest once I discover the reason for 0 degrees Celsius. *NOTE* the calls for the sub routines are a "given" in the assignment which is what has resulted in a loss of precision in the conversion from double to int during the calls...

``````#include <iostream>
#include <cmath>
using namespace std;

double windChill(double v, double t);
//Computes windchill
double f2c(int tempF);
//Converts Farenheit to Celcius
double c2f(int tempC);
//Converts Celcius to Farenheidt

int main( )
{

double t, // t used for temperature in Farenheidt
v, // v used for velocity in meters per second
W; // windchill factor in degrees Celsius

int tempF, // temperature in F used for windchill calculation
tempC; // temperature in C used for windchill calculation;

cout << "Enter the temperature in Farenheit: ";
cin >> t;  // accept t from user
cout << "Enter the wind velocity in meters per second (m/s): ";
cin >> v;  // accept v from user

cout << "temp in F is " << t << endl;
cout << "windspeed is " << v << endl;

tempC =  f2c(t); // call function to convert F to C

cout << "Temp in C is : " << tempC << endl;

if (tempC <= 10) // test if degrees C is less than 10 and execute this portion if true.
{
W = windChill(v, tempC);  // calculate windchill

cout << "Windchill is : " << W << endl;

tempF = c2f(tempC); // convert windchill to Farenheidt

cout << "Temp in F after windchill is : " << tempF << endl;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The windchill is " << W << " degrees Celcius\n";
cout << "and " << tempF << " degrees Farenheidt.";

return(0);
} // end of if

else // return error that degrees in C is less than 10 and windchill is negligiable
{
cout << "The Farenheidt temperature you entered is less than 10 degrees Celcius.\nWindchill is negligable.";
return (0);
}
}
double windChill(double v, double t)
{
double W; //wind chill intex
W = (33-(((10 * sqrt(v))-v+10.5)*(33-t))/23.1);
return W;
}

double f2c(int tempF)
{
double tempC;
tempC = (5/9)*(tempF + 32);
return tempC;
}
double c2f(int tempC)
{
double tempF;
tempF = (9/5)*(tempC - 32);
return tempF;

}``````

## All 6 Replies

In f2c(), 5/9 = 0. In c2f(), 9/5 = 1. Remember the limits of integer math...

In f2c(), 5/9 = 0. In c2f(), 9/5 = 1. Remember the limits of integer math...

Aww, geez. Thank you Walt. I'll type cast those fractions to double values and post back if I have further issues. Thank you for pointing me in the right direction.

The NWS Windchill Temperature (WCT) index uses advances in science, technology, and computer modeling to provide an accurate, understandable, and useful formula for calculating the dangers from winter winds and freezing temperatures. The index:

* Calculates wind speed at an average height of five feet, typical height of an adult human face, based on readings from the national standard height of 33 feet, typical height of an anemometer
* Is based on a human face model
* Incorporates heat transfer theory, heat loss from the body to its surroundings, during cold and breezy/windy days
* Lowers the calm wind threshold to 3 mph
* Uses a consistent standard for skin tissue resistance
* Assumes no impact from the sun (i.e., clear night sky).

Ok.. still not much luck on this. I did correct my conversion from Fahrenheit to Celsius and find a few errors in my windchill function in regards to order of operation and calling the exact same variable twice.. I also fixed my integer division to properly return a double. The windchill conversion is coming up with an incorrect answer according to test against the meteorological calculator I found. Can someone review the attached formula (in .png format) and compare it to my formula in my function "windChill" and see if you can spot the error? I'm relatively certain that my (next) error to fix is in that sub function. If using the inputs of 20 degrees Fahrenheit, and 5 m/s wind speed, the windchill should be -13 and I get -14.8416. In Fahrenheit the answer should be 8 degrees and I am getting 45.6, but until I can figure out my windchill error, there is no sense working on the F conversion as it is based off the other sub routine.

``````#include <iostream>
#include <cmath>
using namespace std;

double windChill(double v, double t);
//Computes windchill
double f2c(double tempF);
//Converts Fahrenheit to Celcius
double c2f(double tempC);
//Converts Celcius to Fahrenheit

int main( )
{

double t, // t used for temperature in Fahrenheit
v, // v used for velocity in meters per second
W; // windchill factor in degrees Celsius

double tempF, // temperature in F used for windchill calculation
tempC; // temperature in C used for windchill calculation;

cout << "Enter the temperature in Fahrenheit: ";
cin >> t;  // accept t from user
cout << "Enter the wind velocity in meters per second (m/s): ";
cin >> v;  // accept v from user

cout << "temp in F is " << t << endl;
cout << "windspeed is " << v << endl;

tempC =  f2c(t); // call function to convert F to C

cout << "Temp in C is : " << tempC << endl; //temp output to test variable "tempC"

if (tempC <= 10) // test if degrees C is less than 10 and execute this portion if true.
{
W = windChill(v, tempC);  // calculate windchill

cout << "Windchill is : " << W << endl; //temp output to test variable "W"

tempF = c2f(tempC); // convert windchill to Fahrenheit

cout << "Temp in F after windchill is : " << tempF << endl; //temp output to test variable tempF after windchill calculation

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The windchill is " << W << " degrees Celcius\n";
cout << "and " << tempF << " degrees Fahrenheit.\n\n";

return(0);
} // end of if

else // return error that degrees in C is less than 10 and windchill is unable to be calculated.
{
cout << "The Fahrenheit temperature you entered is less than 10 degrees Celcius.\nWindchill is unable to be calculated.\n\n";
return (0);
}
}
double windChill(double v, double t)
{
double W; //wind chill index
W = (33-((((sqrt(v)*10)- v + 10.5)*(33-t))/23.1));
return W;
}

double f2c(double tempF)
{
double TempInCelsius;
TempInCelsius = (5/9.0)*(tempF - 32);
return TempInCelsius;
}
double c2f(double tempC)
{
double TempInFahrenheit;
TempInFahrenheit = (9/5.0)*(tempC + 32);
return TempInFahrenheit;

}``````

Before going into windchill, try the experiment, you'd be surprised:

``````tC = f2c(t);
tF = c2f(tC);
cout << "t = " << t << "; tF = " << tF << endl;``````

The printed out values should be identical. They are not.

commented: excelent testing code. I spotted the error in my c2f code almost immediately by using this +1

Before going into windchill, try the experiment, you'd be surprised:

``````tC = f2c(t);
tF = c2f(tC);
cout << "t = " << t << "; tF = " << tF << endl;``````

The printed out values should be identical. They are not.

Thank you. You debugging suggestion worked great and allowed me to see my error in my c2f sub routine almost immediately (order of operations). I also changed my windspeed to call W (the windchill in Celsius) instead of tempC (the temperature in celcius).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.