Reputation: 3163
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;
}
}
Upvotes: 0
Views: 273
Reputation:
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
}
Upvotes: 0
Reputation: 462
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
Upvotes: 2
Reputation: 310990
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
Upvotes: 4