是我的代码更但是编辑现在我卡在成功参数:布尔虚空问题
#include <iostream>
#include <vector> // need this in order to use vectors in the program
using namespace std;
void computeSum (vector<int> &Vec, int howMany, int total, bool success)
//the computeSum function that will sum positive numbers in a vector
{
success = true;
total=0;
if (success){
for(int j=0;j < howMany;j++)
if (Vec[j] > 0){
total+=Vec[j];
} else {
total+=Vec[j+1];
}
return total;
} else {
cerr << "Oops! Appears you cannot add up these numbers!";
}
}
int main()
{
vector<int> dataVec;
int i, n, howMany, total;
cout << "How many numbers would you like to put into the vector? \n";
cin >> n;
dataVec.resize(n);
for(vector<int>::size_type i=0;i < n;i++)
{
cout << "Enter your number for " << i+1 << ": \n";
cin >> dataVec[i];
}
cout << "How many POSITIVE numbers would you like to sum? \n";
cin >> howMany;
cout << "Your total is: \n" << computeSum (dataVec, howMany, total, success);
}
当我编译它,我得到的错误是这样的: - 用一个return语句函数中的值返回void - 那么在这种情况下,if语句中的“返回总数”是否会打印出总数? - 也在int main()函数中说成功没有被声明 - 我将如何去声明它?
你试图超越你的能力。从“Hello World”开始并着手。 – Beta
顺便说一句,你也声明'main'有一个'int'结果,但你永远不会返回任何东西。我怀疑编译器也会对此抱怨。 –
@Daniel:不在C++(或C99)中。落在'main'的末尾会隐含'return 0;'。 –