2

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?

Chachmu
  • 7,725
  • 6
  • 30
  • 35
  • 1
    not a lot of info to go on... what are `cpx`, `cpy`, `play`? – filipe Mar 02 '13 at 00:41
  • I just noticed that both loc[3] and loc[4] ALWAYS give me values that are around 1000 times to large and have a couple of random digits on the very last two digits. I'm not entirely sure what this means but I do know that loc[0] and loc[1] do not have any problems whatsoever. – Chachmu Mar 02 '13 at 00:41
  • @filipe cpx is the current x of my "player" it currently starts at 5 and stays there unless I push a button to move it. cpy is the same only for y and it starts at 0 and goes towards 600 at an accelerating rate. play is the name of the instance of Sprite I am using. I'm using the SFML library. – Chachmu Mar 02 '13 at 00:43

1 Answers1

9

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.

us2012
  • 16,083
  • 3
  • 46
  • 62
  • wow... you totally nailed it... added an endl to the end of that print statement to double check and that solves the problem. Thanks! – Chachmu Mar 02 '13 at 00:45
  • 3
    +1 for your psychic debugging skills. @Roboinventor : `endl` is overkill 99% of the time – see [What is the C++ iostream endl fiasco?](http://stackoverflow.com/q/5492380/636019). – ildjarn Mar 02 '13 at 00:47
  • 2
    @Robo That might not be the right lesson to take away from this - always consider `'\n'` vs `std::endl` depending on whether you want the buffer to be flushed instantly. – us2012 Mar 02 '13 at 00:47
  • thats true but at least for now while I'm just using these statements to help debug it doesn't make much difference – Chachmu Mar 02 '13 at 00:48
  • @perfanoff I checked the code and you are right, it was 70,0,70 rather than 70,700 – Chachmu Mar 02 '13 at 00:50