String Subsript error out of range? HELP ME FIND THE ERROR PLEASE :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const string letters[46][5]=
{
	{				
"   _____   ",						
"  /  _  \\  ",
" /  /_\\  \\ ",
"/    |    \\",
"\\____|__  /" },


{"__________ ",
"\\______   \\",
 "|    |  _//",
" |    |   \\",
" |______  //"},

{enum alphbet {a,b,c,d,e,f,g,


I have this function below that reads a string. Suppose a user string is "AB." the function will read each character and find the character maximum size. Note: I want to find the maximum size of these block letters I have posted above. Furthermore, in my switch case, I have an enumeration data type that will store the character values. I'm going to going that enumeration values into my 2 dimensonial array. My only problem is that I'm getting a subrsipt out of range error on line 106

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
int sizeofblockletters (string usertype)
{//int function start 
	//Declare sum to find the total length of all chaar combined
	int sum=0;
	//Declare variabe to store one character from string
	char store_char_fromusertype;
	//Find lengthof string
	int lengthofstring;
	lengthofstring= usertype.length();

	//Declare variabe to store the largest value of each alphabet string. 
				int largest;
				

	//Declare variable that will store in the the length of row string.
	int highestlengthofcolumn=0;

	//Declare an index that will get each chacter of a string
	for(int index=0;index<lengthofstring;index++)
	{//for loop start parthen...
		//Declare a variable that will convert chars into numbers representing each alphabet. Enumeration will be used
		alphbet convert_alph_tonumber;
		store_char_fromusertype=usertype[i];
			//make a switch structure that wil store each character
				switch (store_char_fromusertype)
								{//Switch structure partenese
		
								case 'a':convert_alph_tonumber=a;
								break;
								case 'b':convert_alph_tonumber=b;
								break;
								case 'c':convert_alph_tonumber=c;
								break;
								case 'd':convert_alph_tonumber=d;
								break;
								case 'e':convert_alph_tonumber=e;
								break;
								case 'f':convert_alph_tonumber=f;
								break;
								case 'g':convert_alph_tonumber=g;
								break;
								case 'h':convert_alph_tonumber=h;
								break;
								case 'i':convert_alph_tonumber=i;
								break;
								case 'j':convert_alph_tonumber=j;
								break;
								case 'k':convert_alph_tonumber=k;
								break;
								case 'l':convert_alph_tonumber=l;
								break;
								case 'm':convert_alph_tonumber=m;
								break;
								case 'n':convert_alph_tonumber=n;
								break;
								case 'o':convert_alph_tonumber=o;
								break;
								case 'p':convert_alph_tonumber=p;
								break;
								case 'q':convert_alph_tonumber=q;
								break;
								case 'r':convert_alph_tonumber=r;
								break;
								case 's':convert_alph_tonumber=s;
								break;
								case 't':convert_alph_tonumber=t;
								break;
								case 'u':convert_alph_tonumber=u;
								break;
								case 'v':convert_alph_tonumber=v;
								break;
								case 'w':convert_alph_tonumber=w;
								break;
								case 'x':convert_alph_tonumber=x;
								break;
								case 'y':convert_alph_tonumber=y;
								break;
								case 'z':convert_alph_tonumber=z;
								break;
								case '@':convert_alph_tonumber=at;
								break;
								case '!':convert_alph_tonumber=exclaim;
								break;
								case '?':convert_alph_tonumber=question;
								break;
								case '.':convert_alph_tonumber=period;
								break;
								case ',':convert_alph_tonumber=com;
								break;
								case '+':convert_alph_tonumber=plus;
								break;
								case '-':convert_alph_tonumber=minus;
								break;
								case '*':convert_alph_tonumber=multiply;
								break;
								case ':':convert_alph_tonumber=col;
								break;
								case '_':convert_alph_tonumber=underline;
								break;
								}//switch structure end parenthesis
	

			//start another for loop to check the largest size of the alphabet images.. For instance if user type in a. Then it wil go to A array and see whats the highest length of that array.
			
				
					highestlengthofcolumn=letters[convert_alph_tonumber][0].length();
					for (int col=1;col<5;col++)
					{
						largest=letters[convert_alph_tonumber][col].length();
							if (largest>=highestlengthofcolumn)
									{
										highestlengthofcolumn=largest;
									}
					}
					//Add up the total length of al character combined
					sum+=highestlengthofcolumn;
				
				
	}//forloop end parthesis

	return (sum)+1;
Last edited on
Anyone please willing to check why I am getting an error on that? Would really appreciate if someone explains it to. Me...
Give us the error, because I don't think many people are willing to read your code !
Two things. You haven't posted all of the relevant code and you don't have a default case in the switch which begins on line 25. Since you didn't post all of the relevant code (or provide a sample string which the function barfs on), it's difficult to say if the lack of a default case in the switch might be a problem.

When posting something which needs debugging, please provide minimal working code which exhibits the problem. Sometimes whittling it down to minimal working code will help you find the problem.
I kind of doubt it's the default :/, but since I'm a beginner then my credibility would be a tad low :D I used another similar function like this with no default and that seemed to work. When I'm running the program in visual studio. It runs perfectly until a error browser pops up saying there is an error with subscript range and it even mentions convert_elph_tonumber...

Also,, is not won't a default in this switch structure because in my program there won't be a case where the user enters something else...
Once again the error is on line 106 for those who qrent aware :)
Also,, is not won't a default in this switch structure because in my program there won't be a case where the user enters something else...

That's impossible to verify from the code you supplied.


Once again the error is on line 106 for those who qrent aware :)

Line 106 is where the error is manifest. It is not necessarily where the error is. What could cause an invalid subscript error on that line? Probably a value assigned to (or not assigned to) convert_alph_tonumber prior to that line, and that is where you should be looking. It's all cause and effect. You should be looking for the cause.
Last edited on

That's impossible to verify from the code you supplied.
Actually, you are correct about that. I thought I had a previous code that will check in if the user entered in the correct entries; however, I don't have anything like that. I guess I will try to mess around with the default and hopefully hoping that I'm doing it in the correct way. By the way this is my other function,
1
2
3
4
5
6
7
8
9
10
11
12
{	//User types in a sentence
	cout<<"\n\nType in a sentence: ";
	cin>>usertypes;

	//Declare index to find the character in a string
	int index=0;
	int track=0;
	//start a for loop that will keep track of each row to be printed
	for (int track=0;track<9;track++)
				{//start of for loop
					//reset index
					index=0;
It does a whole do while loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do
		{//start do while parenthesis
				
					//declare a variable to store in a character. Declare enum type variable to store in a value
						alphbet store_letter_score;
						char letterstore=usertypes[index];
				
							switch (letterstore)
								{//start switch structure parthesis
								case 'a':store_letter_score=a;
								break;........case '_':store_letter_score=underline;
								break;
								default:
									cout<<"Invalid Character Found";
									
									
								}//End switch structure parenthesis 
I did default this way, but don't really believe this would solve the problem. One more thing to note is that the testing string I always enter in is "AB." I have not tested an incorrect string towads this point.

Line 106 is where the error is manifest. It is not necessarily where the error is. What could cause an invalid subscript error on that line? Probably a value assigned to (or not assigned to) convert_alph_tonumber prior to that line, and that is where you should be looking. It's


Going to look over my code again, to see if any irrelevant value was assigned to that variable or not.
I believe I got what my problem is. According to some of the errors I'm getting on visual studio it also says convertalpnumber is being used without initialized. The way I'm thinking is that the convertalpnumber is initialized. For my first code of this post, where the error is being outputted on line 106, I the values of convertalnumber should be 0 and 1 when it process the string "AB."

Also, below my classes, I have an enumeration data type for alphabets set up.
1
2
//Enumerators for alphabets. Values start at zero. Will be using this in  aswitch structure to assign a value in the twodimesonal array, which will cal lthe lette
enum alphbet {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,at,exclaim,question,period,com,plus,minus,multiply,col,underline};
hello?
On line 23 of the sizeofblockletters function you posted above, where does the i come from? Should that not be index?

[edit: Well, I guess I can see where the i comes from. It is part of your alphbet enumeration which gives it the value 8. So if you feed sizeofblockletters a string that is less than 9 characters in length, you would get the reported error. (Which also means that you were reporting the line indicated when the error was generated incorrectly - something I had wondered about since you aren't subscripting a string on line 106.)]
Last edited on
Cire, your absolutely correct about that :D It was suppose to be index and I did seem to run the program fine after fixing that code :D THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!! :)
Topic archived. No new replies allowed.