#include <iostream>
#include <vector>
using namespace std;
double add(vector<double>& sums) {
double result;
for(int i = 1; i <= sums.size(); i++) {
result += sums[i-1];
}
return result;
}
int main(){
.
.
.
vector<double> sums;
.
case '+':
cout << "Type in all the numbers you want to add or substract." << endl;
cout << "Type 0 to stop" << endl;
do {
cin >> n;
sums.insert(sums.begin() + i, n);
i++;
} while (n != 0);
cout << add(sums);
break;
}
I have thus code to take in an unknown amount of numbers from the user and add them. When I run the code without warnings, it works as expected but when I try debugging it I have this segmentation fault. Not only I don't understand why I have segmentation fault even though it is a vector, I also don't know how to fix it. I think it has something to do with pointers, and to be honest I added '&' in function parameter only because I thought it would be more efficient.
I didn't want to share the whole code because it is 160 lines and the other parts are not relevant, but I can share it if necessary.