Int Variable given a random value

In my code (which I can't post til I get home) I am trying to use a for-loop to loop through the values in a vector. My loop looks something like

1
2
3
4
5
cout << "Array size: " << array.size();
for(int i = array.size(); i < 1 ; i--)
{
  blah blah
}

My problem is, my code doesn't go through this loop. When I debug it, the value given to i at the start of the loop is a huge negative number (-147836574 or something) even thought the cout gives me the array size of 1000 (as I wanted). Hence it doesn't loop through, cause its less than zero.

My question .. why is it getting assigned a value that isn't what I specify? I *know* the array is only of size 1000, but it doesn't get this number. It also has a similar problem if I go from zero and loop for the size of the array

1
2
3
4
for(int i = 0; i == array.size(); i++)
{
  blah blah
}

Any obvious ideas as to why this isn't working?

Thanks in advance
In your first loop are you trying to loop through the loop backwards? In any case, in your for loop, i is set to 1000. You are testing to see if i < 1, which 1000 is NOT < 1; therefore, it doesn't enter the loop.

Your second loop, your test of i is NOT = 1000, so that loop isn't entered either.
@kooth

Perhaps my indexing is wrong, I'll have to double check when I get home.

Question though: Why is the assignment giving the loop an initial value other than what I've assigned it? When I debug it, it says i = 1 billion, which is neither 0 nor array.size()

Thanks
Last edited on
Perhaps it's a problem with your compilation. I think maybe you need to look at the assembler to see that the variable is getting set. You might be looking at it before you really are in the loop, then it would be garbage. You could set i to 0 outside of the loop to prove this.
@Kooth

You were right the first time, I think it was with my loop declaration.

I was thinking it was (start conidtion; end condition; iteration) instead of
(start condition; while; iteration)

It was a combination of that and possibly using effectively

array(array.size() )

which obviously is stupid :P

Thanks again :)
No, array.size() is the correct way to go. You simply are overlooking the fact that when you assign i an initial value of 100 and then expect 1 to be less than one, your loop isn't entered. I think you get it now, I just want to make sure. :)
Topic archived. No new replies allowed.