2016-12-12 134 views
-5

這裏是我現在擁有的代碼。就目前而言,它可以完成它所需的一切,以返回必要的相應值。但是,我缺少的一件事是返回與溫度相關的索引號相對應的月份的函數。創建一個字符串函數返回一個月

#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <string> 
#include <cstdlib> 

using namespace std; 

const int NUM_COLS = 2; 
const int NUM_ROWS = 12; 

void getData(ifstream& tempFile, double temps[][NUM_COLS]); 
double averageHigh(double temps[][NUM_COLS], int NUM_ROWS); 
double averageLow(double temps[][NUM_COLS], int NUM_ROWS); 
double indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS); 
double indexLowTemp(double temps[][NUM_COLS], int NUM_ROWS); 
string monthForTemp(double temps[][NUM_COLS], int NUM_ROWS); 

/* 
* 
*/ 
int main(int argc, char** argv) 
{ 
    ifstream tempFile; 
    double temps[NUM_ROWS][NUM_COLS] = {0.0}; 
    tempFile.open("Ch8_ex9Data.txt"); 

    if (!tempFile) 
    { 
     cout << "File does not exist, contact system admin" << endl; 
     return 1; 
    } 
    getData (tempFile, temps); 
    double highAverage = averageHigh(temps, NUM_ROWS); 
    cout << "Average high is: " << highAverage << endl; 

    double lowAverage = averageLow(temps, NUM_ROWS); 
    cout << "Average low is: " << lowAverage << endl; 

    int indexHiTemp = indexHighTemp(temps, NUM_ROWS); 
    cout << "High temp index is: " << indexHiTemp 
     << " which is " << temps[indexHiTemp][0] 
     << " degrees F" << endl; 

    int indexLoTemp = indexLowTemp(temps, NUM_ROWS); 
    cout << "Low temp index is: " << indexLoTemp 
     << " which is " << temps[indexLoTemp][1] 
     << " degrees F" << endl; 
    return 0; 



} 

void getData(ifstream& tempFile, double temps[][NUM_COLS]) 
{ 
    cout << fixed << showpoint << setprecision(1); 
    double hiTemp, loTemp; 

    tempFile >> hiTemp >> loTemp; 

    int rowIndex = 0; 

    while(tempFile) 
    { 
     temps[rowIndex][0] = hiTemp; 
     temps[rowIndex][1] = loTemp; 
     rowIndex++; 
     tempFile >> hiTemp >> loTemp; 
    } 
} 

double averageHigh(double temps[][NUM_COLS], int NUM_ROWS) 
{ 
    double avgHigh = 0.0; 
    double sum = 0.0; 
    int i; 

    for (i = 0; i < NUM_ROWS; i++) 
    { 
     sum += temps[i][0]; 
     //cout << temps[i][0] << endl; 
    } 
    avgHigh = sum/NUM_ROWS; 
    return avgHigh; 
} 

double averageLow(double temps[][NUM_COLS], int NUM_ROWS) 
{ 
    double avgLow = 0.0; 
    double sum = 0.0; 
    int i; 

    for (i = 0; i < NUM_ROWS; i++) 
    { 
     sum += temps[i][1]; 
     //cout << temps[i][1] << endl; 
    } 
    avgLow = sum/NUM_ROWS; 
    return avgLow; 
} 

double indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS) 
{ 
    cout << fixed << showpoint << setprecision(1); 
    double index = 0.0; 
    double maxHighTemp = 0.0; 
    for (int i = 0; i < NUM_ROWS; i++) 
    { 
     if (temps[i][0] > maxHighTemp) 
     { 
      maxHighTemp = temps[i][0]; 
      index = i; 
     } 
    } 
    return index; 
} 

double indexLowTemp(double temps[][NUM_COLS], int NUM_ROWS) 
{ 
    cout << fixed << showpoint << setprecision(1); 
    double index = 0.0; 
    double maxLowTemp = 0.0; 
    string month = " "; 
    for (int i = 0; i < NUM_ROWS; i++) 
    { 
     if (temps[i][0] < maxLowTemp) 
     { 
      maxLowTemp = temps[i][1]; 
      index = i; 
     } 

    } 
    return index, month; 
} 

string monthForTemp(double temps[][NUM_COLS], NUM_ROWS) 
{ 
    string month = " "; 
    switch (index) 
     { 
      case 1 : 
       month = "January"; 
       break; 
      case 2 : 
       month = "February"; 
       break; 
      case 3 : 
       month = "March"; 
       break; 
      case 4 : 
       month = "April"; 
       break; 
      case 5 : 
       month = "May"; 
       break; 
      case 6 : 
       month = "June"; 
       break; 
      case 7 : 
       month = "July"; 
       break; 
      case 8 : 
       month = "August"; 
       break; 
      case 9 : 
       month = "September"; 
       break; 
      case 10 : 
       month = "October"; 
       break; 
      case 11 : 
       month = "November"; 
       break; 
      case 12 : 
       month = "December"; 
       break; 
     } 

    return month; 
} 

我需要的行和列值傳遞給字符串函數,但我不能完全似乎得到它的權利。任何指針?

謝謝。

+0

您的代碼不會編譯,並且您不會將月份'索引'傳遞給函數。 –

+0

對。沒有最後的功​​能,並且沒有「迴歸月」;儘管如此,indexLowTemp的一部分。我試圖找到一種方法將變量傳遞到字符串函數中並獲得一個月作爲響應。 – Ronave4C

+0

這裏顯示的結果: 平均高61.3 平均低40.7 高溫度指數:6是78.2華氏度 低溫指數爲:0是23.7華氏度 我應該讓地方indexLowTemp和indexHighTemp函數也返回一個字符串?或者它應該是一個單獨的功能? – Ronave4C

回答

0
string monthForTemp(double temps[][NUM_COLS], NUM_ROWS) 

被錯誤地聲明,無法執行其任務。它應該是這個樣子

string monthForTemp(int index) 

但要做到這一點,indexHighTemp和指數返回功能的其餘部分需要更改爲類似

int indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS) //returns int 
{ 
    cout << fixed << showpoint << setprecision(1); 
    int index = 0; // index is int 
    double maxHighTemp = 0.0; 
    for (int i = 0; i < NUM_ROWS; i++) 
    { 
     if (temps[i][0] > maxHighTemp) 
     { 
      maxHighTemp = temps[i][0]; 
      index = i; 
     } 
    } 
    return index; 
} 

因爲數組不能使用double S作爲指標。你不能從3.1459獲得數組中的項目。此外,因爲iint,所以index就是double。最好它是一個浮點表示的整數,最壞的情況下(這裏沒有問題,因爲數字很小),它被floating point imprecision損壞。

這裏是一個偉大的pplace到Head into X-Y territory,並提出了完全不同的解決方案:

與陣列替換monthForTemp功能。例如:

std::string monthForTemp[] = 
{ 
    "January", 
    "February", 
    "March", 
    // rest of months go here 
}; 

可作爲

cout << "High temp month is: " << monthForTemp[indexHiTemp] 
    << " which is " << temps[indexHiTemp][0] 
    << " degrees F" << endl; 

假設indexHighTemp已被修改爲返回int如上建議。

相關問題