Vector Help

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";
}

Thanks for the help!

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.

Remove the (4):
vector<int> temps;
Last edited on
When I remove the four I get a range error. How do I avoid this?

Thanks.
By actually adding in four integers. Currently you only ask the user for one.
i dont know much about vectors but doesnt the number have to be in brackets not parentheses
No.
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.
Topic archived. No new replies allowed.