0

For some reason, I'm not able to sum the elements in my vector. When I run the program, the console only prints out the first element. For example, if my input is: 12 23 45 56 (all in one line), the output is 12.

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <numeric>

using namespace std;

int main() {
    vector<int> myVector;

    int numberOfValuesToSum = 10;
    int vectorValues = 0;
    int sum = 0;

//    cout << "Please enter the number of values you want to sum: ";
//    cin >> numberOfValuesToSum;

    cout << "Please enter some integers: ";
    cin >> vectorValues;
    myVector.push_back(vectorValues);

    for (int i : myVector) {
        sum += i;
        cout << sum << endl;
    }
}
Onur-Andros Ozbek
  • 2,998
  • 2
  • 29
  • 78
  • 3
    You call `push_back()` outside of loop ie only once, so you only get one value – Slava Apr 17 '18 at 19:58
  • 1
    I'm guessing you want something like https://stackoverflow.com/questions/22114183/multiple-numbers-input-on-one-line – user3483203 Apr 17 '18 at 20:00
  • You need to tokenize the input to get individual numbers or get each input individually. – Mahesh Apr 17 '18 at 20:00
  • 2
    `vectorValues` is an `int`. It can only ever store a single `int`. That means there is no way for `cin >> vectorValues;` to ever extract more than a single `int` from the input. You should familiarize yourself with a debugger. A good debugger would let you inspect `vectorValues` and `myVector`, which would immediately show you that they both contain only a single value. – François Andrieux Apr 17 '18 at 20:03
  • 1
    Possible duplicate of [How to sum up elements of a C++ vector?](https://stackoverflow.com/questions/3221812/how-to-sum-up-elements-of-a-c-vector) – Jesper Juhl Apr 17 '18 at 20:09
  • [std::accumulate](http://en.cppreference.com/w/cpp/algorithm/accumulate) is all you need. – Jesper Juhl Apr 17 '18 at 20:10
  • @JesperJuhl the issue seems to be with _input_, more than _summing_. – Drew Dormann Apr 17 '18 at 20:11

3 Answers3

4

You need to enter values in the vector in a loop. Also you can use the standard algorithm std::accumulate declared in the header <numeric> to calculate the sum. And you should accumulate values at least in an object of the type long long int because the sum can be a very large number.

Here is a demonstrative program.

#include <iostream>
#include <vector>
#include <numeric>

int main() 
{
    size_t numberOfValuesToSum = 10;
    std::vector<int> v;
    v.reserve( numberOfValuesToSum );

    int vectorValues;

    std::cout << "Enter " << numberOfValuesToSum << " integer numbers: ";
    for ( size_t i = 0; i < numberOfValuesToSum && std::cin >> vectorValues; i++ )
    {
        v.push_back( vectorValues );
    }

    long long int sum = std::accumulate( v.begin(), v.end(), 0ll );

    std::cout << "sum = " << sum << std::endl;

    return 0;
}

Its output might look like

Enter 10 integer numbers: 0 1 2 3 4 5 6 7 8 9
sum = 45
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you Vlad. It worked. I've accepted your answer and have given you an upvote. If you think that my question was well asked, could you please give me an upvote? – Onur-Andros Ozbek Apr 18 '18 at 21:12
2

In the example when 12 23 45 56 is provided it has to be converted to int vectorValues in line cin >> vectorValues;, however vectorValues is an int (single number, not array of numbers (not vector)). Since space is separator, 12 is taken and converted to int.

As consequence vector<int> v contain only one value which is 12, so the sum is also 12.

My proposition for your problem is following, which allow enter arbitrary number of numbers:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> myVector;

    //provide several space separated numbers, accept them by pressing ENTER
    cout << "Please enter some integers: "; 
    while (cin.peek() != '\n') // check user input if ENTER was provided
    {
        int value;
        cin >> value; // convert each provided value from let's say "string" to int
        myVector.push_back(value); // add int to vector
    }

    int sum = 0;
    for (int i : myVector) {
        sum += i;
    }
    cout << "sum = " << sum << endl;

    return sum;
}

Example output:

Please enter some integers: 2 4 8<ENTER>
sum = 14
atrelinski
  • 432
  • 2
  • 9
0

White space seperates values from other values. When you press space, it sees that as ending the first value and so it moves on, waiting until the next statement asking for it. The array adds up all the values it sees (only the first one) and then the program ends. If you want a correct program, do this

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <numeric>

using namespace std;

int main() {
    vector<int> myVector;

    int numberOfValuesToSum = 10;
    int vectorValues = 0;
    int sum = 0;

    cout << "Please enter the number of values you want to sum: ";
    cin >> numberOfValuesToSum;

    int count = 1;     //counter

    while ( numberOfValuesToSum >= count ){
        cout << "Please enter an integer: ";
        cin >> vectorValues;
        cout << "/n"
        myVector.push_back(vectorValues);
        count++;
    }

    for (int i : myVector) {
            sum += i;
            cout << sum << endl;
    }
    cout << "Your sum is: "
    cout << sum
}
Cheesepizza2
  • 31
  • 1
  • 6