String subscript out of range

I'm can't seem to find the thing causing the index on my string to go out of range. I'm trying to read a string like "N2e1E01n0e2e1" and match the numbers to the directions. "E01" would be E or e with a maxSteps of 1. I can't seem to get it to read the last alphabet and number "e1". It reports back the direction and maxSteps fine up to "N2e1E01n0e2" and then I think the out of range error stops it from going on to the "e1".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string path = "N2e1E01n0e2e1",temp;
int index=0,maxSteps;
char dir;

while (index != path.size())
		{
			if (isalpha(path[index]))
			{
				dir=path[index];
				index++;
			}
			if (isdigit(path[index]))
			{
				temp="";
				while (isdigit(path[index]))
				{
					temp+=path[index];
					index++;				
				}
			}
			maxSteps = atoi (temp.c_str());
			cout << dir << " " << maxSteps << endl;
		}


N 2
e 1
E 1
n 0
e 2
Press any key to continue . . .
Last edited on
On line 12, you've incremented the "index" so you're going to be trying to call path[index] when index is out of range. I'd suggest you use a for loop and control the increment of index from there, rather than a while loop (your donig a similar thing again on line 18)
Topic archived. No new replies allowed.