Why does the program crash?

Nov 30, 2011 at 1:11am
Every time i run this program, it crashes, and i don't know why. It might be something obvious but I didn't notice anything wrong.


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


using namespace std;



int main(){

   vector<string> nums(10);
   nums[0] = "zero";
   nums[1] = "one";
   nums[2] = "two";
   nums[3] = "three";
   nums[4] = "four";
   nums[5] = "five";
   nums[6] = "six";
   nums[7] = "seven";
   nums[8] = "eight";
   nums[9] = "nine";
   nums[10] = "ten";

   cout << "Enter a number to be converted to a word";
   int num;
   cin >> num;
   cout << num << " = " << nums[num];



return 0;
}
Nov 30, 2011 at 1:16am
nums[10] doesnt exist. You initialize vector with 10 reserved spots, but nums[10] is trying to access the 11th. Change that (10) up there to (11), and your fine ;) (I think O_o)
Last edited on Nov 30, 2011 at 1:16am
Nov 30, 2011 at 1:18am
Thank you, you were correct!
Nov 30, 2011 at 1:20am
haha no problemo. Always glad to help :D
Topic archived. No new replies allowed.