So here is my code for a sum function for a vector - using boolean success to just pretty much catch simple errors and what not
However when I compile it says that success is undeclared in my int main() and I was just wondering how to properly declare it in my main function
I know having success is kind of annoying - but it needs to be there
#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];
}
cout << total;
} else {
cerr << "Oops! Appears you cannot add up these numbers!";
}
}
int main()
{
vector<int> dataVec;
int i, n, howMany, total;
bool success=true;
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);
}