Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Vector giving incredibly unexpected value

I have this code:

std::vector <int> loc;
loc.push_back(cpx);
loc.push_back(cpy);
loc.push_back(play.GetSize().x);
loc.push_back(70);
std::cout<<loc[3];

in a game I am making but even when I print the value of loc[2] and loc[3] they are completely different than they should be, when I run this code I get loc[3] equaling 70070 instead of 70. Does anybody know how to fix this?

like image 468
Chachmu Avatar asked Nov 29 '25 16:11

Chachmu


1 Answers

You have another cout without an endl or \n somewhere in your code that you forgot to remove. That one prints the 700, while the 70 is the correct output of the last line in your code sample.

Change your last line to std::cout<< " and loc[3] is: " << loc[3] << std::endl; to see that my wild guess is right, then go hunting for that other cout.

like image 89
us2012 Avatar answered Dec 02 '25 05:12

us2012



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!