所以我有麻煩從主函數(一個數組與你輸入的變量)調用數據,並不知道如何將它傳遞給一個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段總。我嘗試了各種方法,其中沒有一個能夠工作。
你有什麼麻煩,在'main'中填充數組,或將數組傳遞給函數? – Beta
如果可能,請使用「std :: vector」而不是數組。現在,您正嘗試在'getTotal'中使用'ARRAYSIZE',但它在'main'中是本地的,所以它在'getTotal'中不可見。您還想給參數指定名稱,以便您可以在'getTotal'中訪問它們。 –
@以上 我有數組傳遞給一個函數的麻煩,這樣我就可以計算出getTotal功能 @Jeffry總,你能舉個例子嗎? –