Reputation: 51
I'm writing a simple program to calculate Heat Index. When I run it, it should check whether the Temp is Fahrenheit or Celcius, and then output the Heat Index. If the Temp is less than 80 degrees it should output " no heat index." right now it is outputting " no heat index for all inputs.
Before, it was outputting -40. for everything. Having a lot of trouble finding out what went wrong here.
#include <iostream>
#include <string>
using namespace std;
void calcheatIndex (float temp, float humidity); // function to execute HI formula
void inputWeather (float temp, float humidity); // function to get data from user
bool moreTemps (); // ask if user wants to calculate more
void temptype (); // F or C
int main () {
float temp = 0;
float humidity = 0;
float heatIndex;
inputWeather(temp, humidity);
calcheatIndex (temp, humidity);
return 0;
}
void inputWeather(float temp, float humidity) {
cout << "Enter temperature: ";
cin >> temp;
cout << "Enter humidity: ";
cin >> humidity;
while (humidity <= 0) {
cout << "Humidity should be greater than 0. ";
cin >> humidity;
}
}
bool moreTemps () {
string answer;
cout << "Calculate another (Y/N) ? ";
cin >> answer;
while (answer != "Y" && answer != "y"
&& answer != "N" && answer != "n") {
cout << "Answer Y/N : ";
cin >> answer;
}
if (answer == "Y" || answer == "y") {
return true;
}
return false;
}
void calcheatIndex (float temp, float humidity) {
const double c1 = -42.379;
const double c2 = 2.04901523;
const double c3 = 10.14333127;
const double c4 = -.22475541;
const double c5 = -0.00683783;
const double c6 = -0.05481717;
const double c7 = 0.00122874;
const double c8 = 0.00085282;
const double c9 = -0.00000199;
double heatIndex = c1 + (c2 * temp) +
(c3 * humidity) +
(c4 * temp*humidity) +
(c5 * (temp*temp)) +
(c6 * (humidity * humidity)) +
(c7 * (temp * temp) * humidity) +
(c8 * temp * (humidity * humidity)) +
(c9 * (temp * temp) * (humidity * humidity));
string type;
cout << "Is this temperature Fehrenhiet or Celcius (F/C) : ";
cin >> type;
while (type != "F" && type != "f" &&
type != "C" & type != "c") {
cout << "Enter F or C :";
cin >> type;
}
if ((type == "F" || type == "f") && temp >= 80.0) { // Fahrenheit and over 80`
cout << heatIndex;
} else if ((type == "C" || type == "c") && temp >= 26.67) {
heatIndex = heatIndex * 9.0 / 5.0 + 32;
cout << heatIndex;
} else {
cout << "no heatIndex" ;
}
}
Upvotes: 1
Views: 2121
Reputation: 23562
C and C++ default to pass-by-value when calling methods (as opposed to pass-by-reference).
In pass-by-value, a copy of the value at the call site is passed in to a new variable in the function; the arguments to the function form a new scope---a new variable with the same name. Since it is a new variable, modificationss to temp
and humidity
in inputWeather
are not reflected in the variables in the main
method. Thus the temp and humidity are zero, which means no heat index. (Before, perhaps you were not initializing temp and humidity so their values were more-or-less random.)
In C++, you can easily change the function to be pass-by-reference by making the signature:
void inputWeather(float &temp, float &humidity) {
This tells the language to make the temp
and humidity
variables inside of inputWeather
refer to the same memory location as the variables that are specified as parameters at the call site, so that modifications to those variables in the function will be visible in the caller's variables.
For more details on the difference, see What's the difference between passing by reference vs. passing by value?
Upvotes: 2
Reputation: 500893
The issue is that inputWeather
is taking its arguments by value:
void inputWeather(float temp, float humidity) {
This means that when the function changes the two variables, the changes don't propagate to the caller. Consequently, calcheatIndex
is always called with temp
and humidity
both set to zero.
One way to fix this is by taking the arguments by reference:
void inputWeather(float& temp, float& humidity) {
(Don't forget to also change the prototype.)
Upvotes: 3