我一直在试图在代码块中运行这个平均的计算器程序,它的构建没有错误,但它不能运行的原因,我不知道为什么。 我的代码如下为什么不会这个程序运行,但它会建立?
#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