for loop and vectors

hello, i am making a simple program that you input numbers and it outputs them once you enter the number 42. i am doing this with vectors so the user can input as many numbers as they want. to output them i use this for loop:

1
2
3
4
for(unsigned int i = 0; i < numbers.size(); i++)
	{
		cout << numbers[i] << endl;
	}


but when run the program it just exits once it gets to here. i did some debugging and the value for i at this point is 34632, or whatever the max number for int is. why dose it do this? how can i fix it?

thanks for the help.
Nothing is wrong with that for loop.

How are you adding the numbers to your vector? With push_back?
i fixed it by moving i into the global scope. but im still wondering what is wrong with the other one. here is all the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <vector>
#include <iostream>

using namespace std;

int main()
{
	vector<int> numbers;
	bool loop = true;
	int number;
	cout << "input numbers." << endl << "input 42 to end" << endl;
	while(loop)
	{
		cin >> number;
		if(number == 42)
		{
			loop = false;
		}
		else
		{
			numbers.push_back(number);
		}
	}
	cout << endl;
	for(unsigned int i = 0; i < numbers.size(); i++)
	{
		cout << numbers[i] << endl;
	}
        system("PAUSE");
	return 0;
}
It worked fine for me. Maybe you are entering a large number that is too big for int?
Topic archived. No new replies allowed.