나는이 평균 계산기 프로그램을 코드 블록에서 실행하려고 시도했지만 오류없이 빌드되지만 어떤 이유로 든 실행할 수 없으며 그 이유를 모른다. 내 코드왜이 프로그램을 실행하지 않습니까?
#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