the second for loop, int a

I got full credit for this assignment but i just came across the second for loop and realized that i may have made a mistake. Take a look at what the int a is being initialized to, in the second for loop, and please let me know if it is 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
  bool isInLanguage_1(string s)
{
	Stack<char> myStack;
	if (s.length() % 2 == 0 || s.at(s.length() / 2) != '$')
	{
		return false;
	}
	else
	{
		{
			for (int a = 0; a < s.length() / 2; a++)
			{
				//char b = aString[a];
				myStack.push(s[a]);

				for (int a = s.at(s.length() / 2) + 1; a < s.length(); a++)
				{
					if (s[a] == myStack.peek())
					{
						myStack.pop();
					}
					else return false;
				}
			}
			return true;
		}
	}
}
Last edited on
I have no idea what your function is supposed to be doing... but yes it appears to be wrong, because in your inner for loop, you're assigning a character of the string to an integer a, and then trying to use that integer as an index. You probably meant to do (a == myStack.peek()). And probably the inner for loop as a whole needs to be reworked because doing a < s.length() doesn't make much sense. Perhaps you just wanted a simple counter?

Just be happy that you got credit, I guess!
Last edited on
Topic archived. No new replies allowed.