我一直在試圖在代碼塊中運行這個平均的計算器程序,它的構建沒有錯誤,但它不能運行的原因,我不知道爲什麼。 我的代碼如下爲什麼不會這個程序運行,但它會建立?
#include <iostream>
using namespace std;
double getAverage(int amount, int numbers[]) {
// Declare the variables
int total = 0;
double avg = 0;
//Find each number in the array then add it to the total
for (int i = amount; i > 0; i--) {
total += numbers[i];
}
//Divide the total by the amount to get the average
avg = total/amount;
cout << "The Average is: ";
//Return the average
return avg;
}
int main() {
// Declare the variables and arrays
int varNum = 1;
int totVar;
int userNums[totVar];
//Ask user for how many variables they want then record it
cout << "How many variables would you like to have? ";
cin >> totVar;
//Ask the user for each variable, then record it into the array
for (int i = totVar; i > 0; i--) {
cout << "Please input variable " + varNum;
cin >> userNums[i];
varNum++;
}
return 0;
}
在創建數組'userNums'之前初始化'totVar'。 – Stefan
它不適合我。 –
將userNums聲明爲std :: vector可以解決許多問題。 – stefaanv