2013-06-26 63 views
1

我在寫一個簡單的程序來計算熱指數。當我運行它時,它應該檢查溫度是華氏度還是攝氏度,然後輸出熱指數。如果溫度低於80度,則應輸出「無熱指數」。現在它正在輸出「爲所有輸入無熱指數。C++程序來計算不提供預期結果的熱指數

之前,它輸出-40。做的一切。有很多的麻煩找出了什麼錯在這裏。

#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" ; 
    } 
} 

回答

3

的問題該inputWeather由價值考慮它的參數:

void inputWeather(float temp, float humidity) { 

這意味着,當該功能改變了兩個變量,所做的更改不會傳播給調用者。因此,calcheatIndex總是調用temphumidity都設爲零。解決這個問題

一種方式是通過參考服用參數:

void inputWeather(float& temp, float& humidity) { 

(不要忘了也改變了原型)

+0

哦哇,謝謝!之前,我對「參照」非常困惑。 – Marla

2

C和C++默認傳址值當調用方法時(而不是通過引用傳遞)。

在傳遞值中,呼叫站點的值的副本被傳遞給函數中的新變量;該函數的參數形成一個新的範圍---一個具有相同名稱的新變量。由於它是一個新變量,因此和humidityinputWeather中的修改不反映在main方法中的變量中。因此,溫度和溼度爲零,這意味着沒有熱指數。 (之前,也許你沒有初始化溫度和溼度,從而它們的值分別爲更多或更少隨機的。)

在C++中,可以很容易地改變功能通過使所述簽名是通過按引用:

void inputWeather(float &temp, float &humidity) { 

這告訴語言做出的inputWeathertemphumidity變量引用相同的存儲單元被指定爲在調用點的參數變量,因此,在功能修改這些變量在調用者的變量中可見。

有關更多細節的區別,請參閱What's the difference between passing by reference vs. passing by value?

+0

謝謝,沒有考慮通過參考! – Marla