"String Subscript Out of Range" Error?

Hello, I am working on a project and I have run into a slight error. I am setting up an array to spit out the ICAO words for a given string. (Ex. Input = "GO" - Output = "Golf Oscar") It compiles cleanly the first time, but after I get the output, I get the error "String Subscript Out of Range". What does this mean and how can I fix it? Thank you very much. I couldn't find any information online that I could understand, frankly, so I hope you guys can help me.

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
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;


int main()
{               
	string stringin;
	int index = 0;
	string ICAO[26] = { "Alpha" , "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", 
						"Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November", 
						"Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform"
						"Victor", "Whiskey", "X-ray", "Yankee", "Zulu" } ;

	do
	{		
	cout << "Please enter string or press 9 to exit:   " << endl;			
	cout << endl;
	cin >> stringin;
	cout << endl;
	cout << "The phonetic version is:   " << endl;
	cout << endl;
	
	for (int i=0; stringin.size(); i++)	
	{
		string stringout;
		char letter;
		letter = toupper(stringin[i]);
		stringout = ICAO[letter-'A'];
		cout << stringout << " ";

	}

	cin.get();
	cin.get();

	} while (stringin != "9");

	return 0;
}
"subscript out of range" means you are indexing an array (or in this case a string), with a bad index. IE, something inside the [brackets] is out of bounds. The debugger SHOULD be snapping and telling you exactly which line of code the problem is on.

To save you some debugging... the problem is that your loop never ends, and i keep increasing until it gets waaaaay to big.

1
2
for (int i=0; stringin.size(); i++)  // <- your condition here was probably supposed to be
           // i < stringin.size() 


As a result... i kept growing and toupper(stringin[i]); was going out of bounds causing the above error.
I DID forget that! It works perfectly now. Thank you so much! :)
Topic archived. No new replies allowed.