".exe has stopped working" "Windows is checking for a solution this problem"

I'm real new to programming and I'm practicing a program dealing with arrays. I go to compile the code and the program runs but then I get an error saying my .exe file has stopped working. Can anyone help please.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

                   
     int main()
     {     
string letters[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k","l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

string numbers[10] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
                     
       cout << "The letters in the alphabet are " << letters[26] << endl;
       cout << "The numbers 0-9 look like this " << numbers[10] << endl;
       cout << sizeof(letters[26]) << endl;
       cout << sizeof(numbers[10]) << endl;
       
                            

       
               system ("PAUSE");
               return 0;
               }
Last edited on
The array string letters[26] has 26 elements. The first element is letters[0] and the last is letters[25].

At line 13, letters[26] is accessed - this is the 27th element. This element is outside the bounds of the array. Whatever data is at that location in memory, it's not likely to be an object of type std::string, and is giving the unpredictable results.

The same out-of bounds access takes place at lines 14, 15 and 16, though I doubt whether the program gets that far.
Last edited on
Topic archived. No new replies allowed.