Problem with Program to Check if Separators Match

This is a discussion on Problem with Program to Check if Separators Match within the C++ Programming forums, part of the General Programming Boards category; Hello, and thank you for taking the time to help me! I am working on a program that reads a ...

Problem with Program to Check if Separators Match

Hello, and thank you for taking the time to help me! I am working on a program that reads a string after the option "E" (enter a string) is chosen, and checks if parentheses (), brackets [], and braces {} are matched in it. There are two files, the main driver file, called Assignment8.cpp, and the utility file, MatchChecker.h. The header file has one class, the MatchChecker class, described below.

The MatchChecker class is a utility class that will be used to check if parentheses (), brackets [], and braces {} are matched in a given string. The MatchChecker class object will never be instantiated. It must have the following public function:

static string matchChecker(string lineToCheck)

The matchChecker function's argument will be a string that can contain parenthesis, brackets, and braces. Other characters or numbers can appears before/after/between them.

If all of them are matched, then the function should return the string:

"Everything is matched!"

If there is a closing parenthesis, bracket, or brace that does not have its corresponding opening parenthesis, bracket, or brace, then for the first such character, return a string with its position such as:

") at the position 15 does not match."/"] at the position 12 does not match."/"} at the position 28 does not match."

The first character of the string is seen as the position 0.

If there is no matching closing parenthesis, bracket, and brace when you reach the end of the string after checking each character, return a string notifying the last opening parenthesis, bracket, or brace that did not have its matching closing one as:

") is missing."/"] is missing."/"} is missing."

Requirements:

You need to implement this function using the stack using stack.h in C++ STL(Standard Template Library).

You can use the string "A" as a token for parentheses, "B" as a token for brackets, "C" as a token for braces.

So, as you can see, its not a very complex program, but I have never worked with stacks before, so its rough for me. I have a good amount of the code written, and everything is compiling fine, but when I enter a string and then press the Enter key, the program has an error and Windows forces me to terminate the program.

Here my files, and they are also attached so that you can run them if you want.

You are in a very bad situation because the parenthesis are not matching in your program

What do you mean? Are there unmatched parentheses in my code? I can't find any, but I don't know exactly what you mean here.

Originally Posted by anon

There are other problems: it's stack in the header <stack>, there is no Character type, strings don't have a charAt method, you are using variables you haven't declared etc.

Okay...I understand what you mean about all this. I am used to working in other languages, mainly just plain C, and this is only my third or fourth program in C++, so I am still getting used to the syntax of everything.

Originally Posted by anon

Also

Code:

Stack<Character> myStack = new Stack<Character>();

This is not C#. You create a stack on the stack, and it looks like this:

Code:

stack<char> myStack;

Okay, I fixed this. Like I said, I am still getting used to the syntax of things, and I have never worked with stacks before, so this is all brand new to me.

Originally Posted by anon

It almost looks like you have copied some C# code from somewhere (using a class with only static functions looks like another C#ism).

Well, actually, the fact that the class has only static functions is due to the material that was provided to me by my teacher. She gave us the function headers, so they are supposed to be static, so I think that is okay here. Will it still work okay?

Okay, I did some work on the errors I had, and I have a new version of the code that only has 1 error, and 2 warnings. Here are those:

MatchChecker.h|20|warning: comparison between signed and unsigned integer expressions|
MatchChecker.h|35|error: void value not ignored as it ought to be|
MatchChecker.h|50|warning: no return statement in function returning non-void|

Now, I don't think the first warning really matters - does it? I have no clue what the error means, so I don't know what to do about that. And I think the second warning is important, but I don't know what to return for my function. Do I change my cout statements to return statements to take care of that?

So your matchChecker function is supposed to return a string. See that "string" right there at the front? You must return a string. And apparently you've broken your pop function, because pop shouldn't be a void function (it should presumably return whatever your stack is a stack of).

So your matchChecker function is supposed to return a string. See that "string" right there at the front? You must return a string. And apparently you've broken your pop function, because pop shouldn't be a void function (it should presumably return whatever your stack is a stack of).

Okay, that's what I thought, but I don't know how to get it to return the string I want. I need to concatenate stuff to the string, so I tried using the + operator, but I got an error (shown below). How do I concatenate the strings I need to join?

Also, how do I fix the pop function? I mean, I know you said I broke it, but what does that mean? How does something like this happen? Or am I not using it right, and that is what you meant?

You can only use + on actual strings, not on char arrays. Is there a reason you are using char arrays?

Oh, okay. No, there isn't a reason, besides the fact that I don't know how to alter things so that I am using actual strings rather than char arrays. How would I change things to use strings instead of char arrays?

Why are you using a class?
Why, if you are using a class, are you not providing class-scope storage?
Why are you actually writing the definition for the only function in your class in the header?
Also, for your class, you aren't creating a constructor.

Looking at that, I'd start by just breaking things down into simple functions first.

It seems you are trying to do way too much here in one function, and not really understanding what.

Oh, okay. No, there isn't a reason, besides the fact that I don't know how to alter things so that I am using actual strings rather than char arrays. How would I change things to use strings instead of char arrays?

Thanks for the help, tabstop!! :-)

Type "string" everywhere you want a string. Make sure you include <string>.

Hmm...every time I try putting string instead of char, I get even more errors, shown below. Do I need to use .compare now because they are strings? I tried that at first, and it didn't work, but maybe I should try it again. Thanks for helping me with this, tabstop!

Okay, I have worked on the program some more, but I still can't figure out how to get it to do what I want. Right now, I am using strcmp to compare the strings, but I am getting an error (multiple times) that makes me think I am not using it right (see below). Here are the errors I am getting right now:

You can switch on a single character, not a string. You can't use strcmp on a string, you use ==.

Okay...so I created a new variable, a character, which I am initializing to the same thing as ch, which is lineToCheck[j], and running my switch on that. Also, I uncommented the other if statement that I had, which uses == instead of strcmp, but now I get these errors that make it seem like I can't use == either. So I don't know what to do now.

Alright, I changed my single quote marks to doubles, so that the characters were being read as strings, and now I am not getting those errors about the == operator. I am still getting the errors about the + operator, though, so I am unsure about how to concatenate the strings that I have now. How do I do that?

Also, I am still getting that error about my pop function. What in the world is going on with that? Is there something I have done wrong that is making it do this?