So I have this code that has a sum function that sums n elements in a vector
...
void computeSum (vector<int> &Vec, int howMany, int total, bool success)
//the computeSum function that will sum positive numbers in a vector
{
int j;
total=0;
for(j=0;j < howMany;j++)
if (Vec[j] > 0){
total+=Vec[j];
} else {
total+=Vec[j+1];
}
if (howMany > Vec[j])
success = false;
else
success = true;
if (total != 0)
success = true;
if (success=true){
cout << total;
} else {
cout << "Oops! Appears you cannot add up these numbers!";
}
}
int main()
{
vector<int> dataVec;
bool success;
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 " << computeSum(dataVec, howMany, total, success);
}
Now before I added the
so now I declared it and I get this...
g++ -I/usr/local/include/bjarne/GUI -I -Wall -O3 -DNDEBUG -Wno-deprecated proj1.cc -o proj1
proj1.cc: In function ‘int main()’:
proj1.cc:59:76: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & std::cout), ((const char*)"Your total is ")) << computeSum((* & dataVec), howMany, total, ((int)success))’
proj1.cc:59:76: note: candidates are:
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:110:7: note: no known conversion for argument 1 from ‘void’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&)’
...etc.
....What?
What's the problem?
**I recently added a return 0; value at the end of int main() but still gives me the same problem
void computeSum (vector<int> &Vec, int howMany, int total, bool success)
computeSum returns a void i.e. it does not return anything. How do you expect to output absolutely nothing in the line cout << "Your total is " << computeSum(dataVec, howMany, total, success);?
This line, cout << "Your total is " << computeSum(dataVec, howMany, total, success);
essentially says "Output 'Your total is' and then output the returned value from this function call".
Additionally, you're calling computeSum and passing it to cout, but computeSum itself needs to make use of cout internally in the line cout << total; or cout << "Oops! Appears you cannot add up these numbers!"; This is not sensible.
But now I have a further question for my computeSum()
I have written
if (howMany > Vec[j])
success = false;
however, this apparently isn't working out for me
I would like for it that when someone enters say: 3 numbers
ex: 1, -2, 3
And wants to add all these numbers they get an error message
cout << "Oops! Appears you cannot add up these numbers!"
Since the function only adds POSITIVE integers
I have the loop that sums the positive integers - but I dont want all the positive ints in the vector totalled only the first couple of positive ints specified by the user (howMany)