为什么这个程序不会 运行,但它会构建?
Why wont this program run, but it will build?
我一直在尝试在代码块中 运行 这个普通的计算器程序,它构建时没有错误,但由于某种原因它不能 运行,我不知道为什么。
我的代码如下
#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;
}
请参阅:@Pete Becker 以获得实际答案
创建数组前需要初始化totVar
userNums
您正在使用
cin >> totVar;
稍后在您的软件中您可能想要给它一个上限。
int userNums[1000];
并检查 totVar
是否执行 999。
此代码存在 三个 个问题。 First,正如@stefan 所说,totVar
用作数组大小时尚未初始化。但这并不重要,因为 second、int userNums[totVar];
不是合法的 C++(它编译是因为 GCC 扩展)。还有第三个,那些循环
for (int i = totVar; i > 0; i--) {
cout << "Please input variable " + varNum;
cin >> userNums[i];
varNum++;
}
和
for (int i = amount; i > 0; i--) {
total += numbers[i];
}
将无效索引传递给数组。大小为 N 的数组具有从 0 到 N-1 的有效索引。第一次通过第一个循环访问 numbers[totVar]
,它位于数组的末尾。像第一个一样编写循环的通常方法是
for (int i = 0; i < totVar; ++i)
cin >> userNums[i];
这将访问 numbers[0]
、numbers[1]
、... numbers[totVar-1]
处的值。
对第二个循环做同样的事情。
我一直在尝试在代码块中 运行 这个普通的计算器程序,它构建时没有错误,但由于某种原因它不能 运行,我不知道为什么。 我的代码如下
#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;
}
请参阅:@Pete Becker 以获得实际答案
创建数组前需要初始化totVar
userNums
您正在使用
cin >> totVar;
稍后在您的软件中您可能想要给它一个上限。
int userNums[1000];
并检查 totVar
是否执行 999。
此代码存在 三个 个问题。 First,正如@stefan 所说,totVar
用作数组大小时尚未初始化。但这并不重要,因为 second、int userNums[totVar];
不是合法的 C++(它编译是因为 GCC 扩展)。还有第三个,那些循环
for (int i = totVar; i > 0; i--) {
cout << "Please input variable " + varNum;
cin >> userNums[i];
varNum++;
}
和
for (int i = amount; i > 0; i--) {
total += numbers[i];
}
将无效索引传递给数组。大小为 N 的数组具有从 0 到 N-1 的有效索引。第一次通过第一个循环访问 numbers[totVar]
,它位于数组的末尾。像第一个一样编写循环的通常方法是
for (int i = 0; i < totVar; ++i)
cin >> userNums[i];
这将访问 numbers[0]
、numbers[1]
、... numbers[totVar-1]
处的值。
对第二个循环做同样的事情。