Whenever I try to input integers into this vector it does not work. When the vector prints it prints with only zeros. How do I get the vector to contain the integers I input?
int main()
{
cout<<"Please enter 4 temperatures separated by spaces\n";
vector<int>temps(4);
int temp;
cin>>temp;
temps.push_back(temp);
cout<<"Your temperatures are:";
for(int i=0; i<4; ++i){
cout<<temps[i]<<" ";
}
cout<<"\n";
}
vector<int>temps(4);
This makes a vector with four zeros in it. You then add one number to the end, and then display the firt four elements which are zero.
Just to clarify, accessing a vector is similar to accessing an array. You do not want to go out of bounds. If you default construct an empty vector and push back 1 element, you should only attempt to print 1 element.