2013-10-21 73 views
0

所以我有麻煩從主函數(一個數組與你輸入的變量)調用數據,並不知道如何將它傳遞給一個float getTotal函數。這裏是我的代碼:你將如何從main調用數據並將其傳遞給函數? (C++)

#include <iostream> 
#include <iomanip> 

using namespace std; 

float getTotal(float [], int) 
    { 
    double total = 0; 
    for (int i=1; i<ARRAYSIZE; i++) 
     { 
      total += inputs[i]; 
     } 
    cout << "The total rainfall for the year is " << total << "inches." << endl; 
    return total; 
    } 

float getAverage(float [], int) 
    { 
    //code goes here 
    } 

int main() 
    { 
    const int ARRAYSIZE = 13; 
    int inputs[ARRAYSIZE], i=1; 
    do 
     { 
      cout << "Enter the rainfall (in inches) for month #" << i << ": "; 
      cin >> inputs[i]; 
      if (inputs[i] < 0) 
       { 
        cout << "Please enter a non-negative number for rainfall in month " << i << " :"; 
        cin >> inputs[i]; 
       } 
      i++; 
     } 
    while (i < 13); 

    float getTotal(float inputs[], int ARRAYSIZE); 
    float getAverage(float inputs[], int ARRAYSIZE); 
    } 

所以我想打電話從主陣列數據和計算的getTotal段總。我嘗試了各種方法,其中沒有一個能夠工作。

+0

你有什麼麻煩,在'main'中填充數組,或將數組傳遞給函數? – Beta

+2

如果可能,請使用「std :: vector」而不是數組。現在,您正嘗試在'getTotal'中使用'ARRAYSIZE',但它在'main'中是本地的,所以它在'getTotal'中不可見。您還想給參數指定名稱,以便您可以在'getTotal'中訪問它們。 –

+0

@以上 我有數組傳遞給一個函數的麻煩,這樣我就可以計算出getTotal功能 @Jeffry總,你能舉個例子嗎? –

回答

0
float getTotal(float inputs[], int ARRAYSIZE) 
    { 
    double total = 0; 
    for (int i=1; i<ARRAYSIZE; i++) 
     { 
      total += inputs[i]; 
     } 
    cout << "The total rainfall for the year is " << total << "inches." << endl; 
    return total; 
    } 

float getAverage(float inputs[], int ARRAYSIZE) 
    { 
    //code goes here 
    } 

int main() 
    { 
    const int ARRAYSIZE = 13; 
    int inputs[ARRAYSIZE], i=1; 
    do 
     { 
      cout << "Enter the rainfall (in inches) for month #" << i << ": "; 
      cin >> inputs[i]; 
      if (inputs[i] < 0) 
       { 
        cout << "Please enter a non-negative number for rainfall in month " << i << " :"; 
        cin >> inputs[i]; 
       } 
      i++; 
     } 
    while (i < 13); 

    float fTotal = getTotal(inputs, ARRAYSIZE); 
    float fAverage = getAverage(inputs, ARRAYSIZE); 
    } 
+0

這裏是我的新的和更新的代碼,將粘貼我得到的錯誤。我不確定爲什麼會發生這種情況? 代碼:http://puu.sh/4VqJv.png 我得到的錯誤此puu.sh鏈接上找到。 http://puu.sh/4VqHi.png –

+0

編輯:錯誤在這裏,更新並修復了一個小錯誤。 http://puu.sh/4VqNI.png –

+0

你沒有在函數getTotal中聲明我。你的錯誤是因爲你不瞭解範圍。這是一個快速的[參考](http://msdn.microsoft.com/en-us/library/b7kfh662.aspx)。一般規則是變量只在{}塊內可見,所以其他函數不能使用在主功能塊中聲明的任何變量。 – PovilasG

0

更換你的函數聲明:

float getTotal(float [], int) 
float getAverage(float [], int) 

這個

float getTotal(int* inputs, int ARRAYSIZE) 
float getAverage(int* inputs, int ARRAYSIZE) 

而此行的main()函數中:

float getTotal(float inputs[], int ARRAYSIZE); 
float getAverage(float inputs[], int ARRAYSIZE); 

這個

float getTotal(inputs, ARRAYSIZE); 
float getAverage(inputs, ARRAYSIZE); 
+0

我一樣,得到了一個錯誤,所以我與此 \t浮子fTotal = getTotal(輸入,ARRAYSIZE)改變了代碼; \t浮子fAverage = getAverage(輸入,ARRAYSIZE); 得到「現在不能將參數1從int [13]轉換爲float []錯誤 –

+0

對不起,我編輯了我的答案,讓我知道如果編譯器仍然吐出錯誤,我目前沒有編譯器:-( –

+0

沒問題!我很欣賞的幫助!它似乎有工作,但我必須確認之前解決以前的錯誤 錯誤我現在得到的是錯誤C2078:太多的初始值設定 只有錯誤離開 這是上線浮getTotal(輸入ARRAYSIZE); –

0

我們的源代碼有太多的錯誤。例如「int inputs [ARRAYSIZE]」。您的數組 是int數據類型,但是您將此數組作爲float數據類型傳遞。有一個編譯器
錯誤。其次,你應該在主函數之外聲明函數和它的數據類型,但是你在主函數 裏面做的
float getTotal(float inputs [],int ARRAYSIZE); 還有另一個編譯器錯誤。在調用函數時,您不需要指定參數的數據類型 。所以我寫了一個可能的代碼,希望它能幫助你。

#include <iostream> 
#include <iomanip> 

using namespace std; 

float getTotal(int *inputs, int ARRAYSIZE) 
    { 
    double total = 0; 
    for (int i=1; i<=ARRAYSIZE; i++) 
     { 
     total += inputs[i]; 
     } 
     cout << "The total rainfall for the year is " << total << "inches." << endl; 
    return total; 

}

float getAverage(int *inputs, int ARRAYSIZE) 
    { 
    //code goes here 

    return 1.4; 
    } 

int main() 
    { 
    const int ARRAYSIZE = 3; 
    int inputs[ARRAYSIZE], i=1; 
    do 
     { 
     cout << "Enter the rainfall (in inches) for month #" << i << ": "; 
     cin >> inputs[i]; 
     if (inputs[i] < 0) 
      { 
      cout << "Please enter a non-negative number for rainfall in month " << i  
       << " :"; 
       cin >> inputs[i]; 
      } 
     i++; 
    } 
while (i <= 3); 

    getTotal(inputs, ARRAYSIZE); 
    getAverage(inputs, ARRAYSIZE); 

return 0; 

}

 While calling function, the return value you can store in variable but data type of 

     variable should be the same of return type of the function 

     e.g float a = getTotal(inputs, ARRAYSIZE); 
    float b = getAverage(inputs, ARRAYSIZE); 
     or you can directly call this function in cout output command statement.like 

COUT < < getTotal(輸入,ARRAYSIZE);

+0

嗯,我所擁有的一切工作,除了從int [13]轉換參數1漂浮[]任何幫助。有沒有? –

+0

我想你需要看到這個鏈接。希望它對你有用[鏈接](htt電話號碼://stackoverflow.com/questions/12344400/convert-int-to-float-in-c-no-casting) –

0

喜@ user2901840的代碼稍加修改允許它運行良好:

#include <iostream> 
#include <iomanip> 

using namespace std; 

float getTotal(float floatarray[], int size) 
{ 
    float total = 0.0f; 
    for (int i=0; i<size; i++) 
    { 
     total += floatarray[i]; 
    } 
    return total; 
} 

float getAverage(float floatarray[], int size) 
    { 
     return (getTotal(floatarray, size))/size; 
    } 

int main() 
    { 
     int ARRAYSIZE = 12, i=0; 
     float inputs[ARRAYSIZE]; 
     do 
     { 
      cout << "Enter the rainfall (in inches) for month #" << i << ": "; 
      cin >> inputs[i]; 
      if (inputs[i] < 0.0) 
       { 
        cout << "Please enter a non-negative number for rainfall in month " << i << " :"; 
        cin >> inputs[i]; 
       } 
      i++; 
     } 
     while (i < ARRAYSIZE); 
     cout << "\n\nThe total rainfall for the year is " << getTotal(inputs, ARRAYSIZE) << " inches." << endl; 
     cout << "The average rainfall per month during the year was: " << getAverage(inputs, ARRAYSIZE) << " inches."<< endl; 
    } 

您需要正確的預定義類,並在需要調用進行修改。爲了說明我有的代碼: - 重新編號以匹配常見C++約定(對12個值運行0到11)的i值 - 重新使用函數以最小化代碼 - 將數組重新聲明爲浮點數組最初有它作爲整數數組)

希望這有助於:)

讓我知道,如果你需要更多的細節和解釋,我可以在更多的信息填寫。

相關問題