Scary error :@@

Hi helppp!! I'm getting this annoying error.. trying to compute a simple code, i'm doing all the right things;

I don't understand!!!!;

http://puu.sh/1FcFI

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;


	int main()
	{
	vector<int> num;
	int d = 0;
	while ( d < 2)
	{	
		cin >> d;
		num.push_back (d);
		d++;
	}
	if (num [0] > num[1] ){
			
			cout << num[0] << " is larger than " << num[1] << endl;
		
	}else if (num [0] < num [1]){
		
			cout << num[0] << " is smaller than " << num[1];
		}
	//else throw domain_error("Fail");
	return 0;
}


my head hurts and this is probably some of the easiest codes one would code up but aadfja;klfjsalhto;ajdska
Before you access num[1], you need to be sure that the vector contains at least two elements.

At line 14, if the user enters a positive integer, it is stored in the vector. Then the loop terminates, containing just one element. Under that condition, you will get an error when accessing the second element.
Okey, thanks for the reply, erm I changed the while loop to while (d < 3). The error still comes up - a different one now I think. http://puu.sh/1Fe2w

What do I do to make sure the cin takes in enough vectors before I access num[1] ?

Last edited on
Use the counter as a counter.
1
2
3
4
5
6
7
	while ( d < 2)
	{	
		int another_variable_that_it_is_not_the_counter_of_the_loop;
		cin >> another_variable_that_it_is_not_the_counter_of_the_loop;
		num.push_back (another_variable_that_it_is_not_the_counter_of_the_loop);
		d++;
	}
Ooooo.. thank you :)
Topic archived. No new replies allowed.