So once I enter my first input, the error appears. I don't know what is the issue. Can anyone help?
vector<int> get_data(){
vector<int> v; //initialize the vector (vec)
int temp;
cin >> temp; //get input (temp)
while(isdigit(temp) == true){ //check input (while(isnumeric(temp))
v.push_back(temp); //if true store in vector
cin >> temp;
}
return v;
}
/*void print (vector<int> vec){
//for loop
//get counter of the vec(length)
//
}*/
int sum (vector<int> vec){
float sum;
for(int i = 0; i <= vec.capacity()-1; i++){
sum += vec[i];
}
return sum;
}
int main(){
vector<int> data_main;
cout << "\nPlease enter your data." << endl;
cout << "You can enter nondecimal words to notify the end of your data." << endl;
data_main = get_data();
//print(data_main);
cout << "The sum of the vector elements is " << sum(data_main);
return 0;
}
This is what happens:
Please enter your data.
You can enter nondecimal words to notify the end of your data.
10
zsh: segmentation fault