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 ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    78

    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.

    Code:
    // Assignment #: 8
    // Name: Your name
    // EmailAddress:
    // Description: It displays a menu of choices to a user
    //        and performs the chosen task. It will keep asking a user to
    //      enter the next choice until the choice of 'Q' (Quit) is entered.
    
    #include <iostream>
    #include "MatchChecker.h"
    
    using namespace std;
    
    void printMenu();
    
    int main()
    {
    	char input1;
    	string line;
    	string inputInfo;
    	MatchChecker checker1;
    
    	printMenu();
    
    
    	do  // will ask for user input
    	{
    		cout << "What action would you like to perform?" << endl;
    		cin >> input1;
    		cin.ignore(30, '\n');
    
    		// matches one of the case statements
    		switch (input1)
    		{
    		case 'E':   //Enter String
    			cout << "Please enter a string.\n";
    			getline(cin, inputInfo, '\n');
    			checker1.matchChecker(inputInfo);
    			break;
    		case 'Q':   //Quit
    			break;
    		case '?':   //Display Menu
    			printMenu();
    			break;
    		default:
    			cout << "Unknown action\n";
    			break;
    		}
    	} while (input1 != 'Q' && input1 != 'q');
    
    	return 0;
    }
    
    
    /** The method printMenu displays the menu to a user**/
    void printMenu()
    {
    	cout << "Choice\t\tAction\n";
    	cout << "------\t\t------\n";
    	cout << "E\t\tEnter String\n";
    	cout << "Q\t\tQuit\n";
    	cout << "?\t\tDisplay Help\n\n";
    }
    Code:
    #include <stdio.h>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    
    // protections
    #ifndef MatchChecker_H
    #define MatchChecker_H
    
    using namespace std;
    
    class MatchChecker
    {
          public:
          static string matchChecker(string lineToCheck)
          {
                 Stack<Character> myStack = new Stack<Character>();
    
                 for (int j = 0; j < input.length(); j++) 
                 {
                     char ch = input.charAt(j);
                     switch (ch) 
                     {
                            case '{': 
                            case '[':
                            case '(':
                                 theStack.push(ch);
                                 break;
                            case '}': 
                            case ']':
                            case ')':
                                 if (!theStack.isEmpty()) 
                                 {
                                     char chx = theStack.pop();
                                     if ((ch == '}' && chx != '{') || (ch == ']' && chx != '[') || (ch == ')' && chx != '('))
                                     cout << "Error: " << ch << " at the position " << j << " does not match" << endl;
                                 } 
                                 else
                                     cout << "Error: " << ch << " at the position " << j << " does not match" << endl;
                                 break;
                            default:
                                 break;
                     }
                 }
                 if (!theStack.isEmpty()){
                 cout << "Error: missing right delimiter" << endl;
          }               
    };
    
    #endif
    Attached Files Attached Files

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are in a very bad situation because the parenthesis are not matching in your program!!

    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.

    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;
    It almost looks like you have copied some C# code from somewhere (using a class with only static functions looks like another C#ism).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Quote Originally Posted by anon View Post
    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.


    Quote Originally Posted by anon View Post
    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.


    Quote Originally Posted by anon View Post
    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.


    Quote Originally Posted by anon View Post
    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?

    I am getting a few errors when I compile my code (below) now. They say this:
    MatchChecker.h|20|warning: comparison between signed and unsigned integer expressions|
    MatchChecker.h|22|error: invalid conversion from `char' to `const char*'|
    \MatchChecker.h|22|error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
    MatchChecker.h|23|error: switch quantity not an integer|
    MatchChecker.h|28|error: no matching function for call to `std::stack<char, std::deque<char, std::allocator<char> > >:ush(std::string&)'|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\c++\3.4.5\bits\stl_stack.h|1 91|note: candidates are: void std::stack<_Tp, _Sequence>:ush(const typename _Sequence::value_type&) [with _Tp = char, _Sequence = std::deque<char, std::allocator<char> >]|
    MatchChecker.h|33|error: 'class std::stack<char, std::deque<char, std::allocator<char> > >' has no member named 'isEmpty'|
    MatchChecker.h|35|error: conversion from `void' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested|
    MatchChecker.h|46|error: 'class std::stack<char, std::deque<char, std::allocator<char> > >' has no member named 'isEmpty'|
    MatchChecker.h|50|warning: no return statement in function returning non-void|


    Here is my updated MatchChecker function:

    Code:
    #include <stdio.h>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    #include <stack>
    
    // protections
    #ifndef MatchChecker_H
    #define MatchChecker_H
    
    using namespace std;
    
    class MatchChecker
    {
          public:
          static string matchChecker(string lineToCheck)
          {
                 stack<char> myStack;
    
                 for (int j = 0; j < lineToCheck.length(); j++)
                 {
                     string ch = lineToCheck[j];
                     switch (ch)
                     {
                            case '{':
                            case '[':
                            case '(':
                                 myStack.push(ch);
                                 break;
                            case '}':
                            case ']':
                            case ')':
                                 if (!myStack.isEmpty())
                                 {
                                     string popper = myStack.pop();
                                     if (((ch.compare("}") == 0) && popper.compare("{") == 0) || ((ch.compare("]") == 0) && popper.compare("[") == 0) || ((ch.compare(")") && popper.compare("(") == 0)))
                                     cout << "Error: " << ch << " at the position " << j << " does not match" << endl;
                                 }
                                 else
                                     cout << "Error: " << ch << " at the position " << j << " does not match" << endl;
                                 break;
                            default:
                                 break;
                     }
                 }
                 if (!myStack.isEmpty())
                 {
                 cout << "Error: missing right delimiter" << endl;
                 }
          }
    };
    
    #endif
    Last edited by vileoxidation; 10-12-2009 at 04:48 PM.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    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?

    Here is my code, which is also attached:

    Code:
    #include <stdio.h>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    #include <stack>
    
    // protections
    #ifndef MatchChecker_H
    #define MatchChecker_H
    
    using namespace std;
    
    class MatchChecker
    {
          public:
          static string matchChecker(string lineToCheck)
          {
                 stack<char> myStack;
    
                 for (int j = 0; j < lineToCheck.length(); j++) // line 20
                 {
                     char ch = lineToCheck[j];
                     switch (ch)
                     {
                            case '{':
                            case '[':
                            case '(':
                                 myStack.push(ch);
                                 break;
                            case '}':
                            case ']':
                            case ')':
                                 if (!myStack.empty())
                                 {
                                     char popper = myStack.pop();  // line 35
                                     if (((ch == '}') && popper == '{') || ((ch == ']') && (popper == '[')) || ((ch == ')') && (popper == '(')))
                                     cout << "Error: " << ch << " at the position " << j << " does not match" << endl;
                                 }
                                 else
                                     cout << "Error: " << ch << " at the position " << j << " does not match" << endl;
                                 break;
                            default:
                                 break;
                     }
                 }
                 if (!myStack.empty())
                 {
                 cout << "Error: missing right delimiter" << endl;
                 }
          }
    };
    
    #endif
    Attached Files Attached Files

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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).

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Quote Originally Posted by tabstop View Post
    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?

    Okay, so here are the errors I am still getting:

    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|38|error: invalid operands of types `const char*' and `const char[18]' to binary `operator+'|
    MatchChecker.h|44|error: invalid operands of types `const char*' and `const char[18]' to binary `operator+'|


    And here is my code:

    Code:
    #include <stdio.h>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    #include <stack>
    
    // protections
    #ifndef MatchChecker_H
    #define MatchChecker_H
    
    using namespace std;
    
    class MatchChecker
    {
          public:
          static string matchChecker(string lineToCheck)
          {
                 stack<char> myStack;
    
                 for (int j = 0; j < lineToCheck.length(); j++) // line 20
                 {
                     char ch = lineToCheck[j];
                     switch (ch)
                     {
                            case '{':
                            case '[':
                            case '(':
                                 myStack.push(ch);
                                 break;
                            case '}':
                            case ']':
                            case ')':
                                 if (!myStack.empty())
                                 {
                                     char popper = myStack.pop();  // line 35
                                     if (((ch == '}') && popper == '{') || ((ch == ']') && (popper == '[')) || ((ch == ')') && (popper == '(')))
                                     {
                                        string returnValue = "Error: " + ch + " at the position " + j + " does not match";
                                        return returnValue;
                                     }
                                 }
                                 else
                                 {
                                     string returnValue = "Error: " + ch + " at the position " + j + " does not match";
                                     return returnValue;
                                 }
                                 break;
                            default:
                                 break;
                     }
                 }
                 if (!myStack.empty())
                 {
                 return ("Error: missing right delimiter");
                 }
          }
    };
    
    #endif

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can only use + on actual strings, not on char arrays. Is there a reason you are using char arrays?

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Quote Originally Posted by tabstop View Post
    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?

    Thanks for the help, tabstop!! :-)

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    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.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by vileoxidation View Post
    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>.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    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!

    Here are the errors:
    MatchChecker.h|22|error: invalid conversion from `char' to `const char*'|
    MatchChecker.h|22|error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
    MatchChecker.h|23|error: switch quantity not an integer|
    MatchChecker.h|35|error: conversion from `void' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested|
    MatchChecker.h|36|error: no match for 'operator==' in 'ch == '}''|
    MatchChecker.h|36|error: no match for 'operator==' in 'popper == '{''|
    MatchChecker.h|36|error: no match for 'operator==' in 'ch == ']''|
    MatchChecker.h|36|error: no match for 'operator==' in 'popper == '[''|
    MatchChecker.h|36|error: no match for 'operator==' in 'ch == ')''|
    MatchChecker.h|36|error: no match for 'operator==' in 'popper == '(''|
    MatchChecker.h|38|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|
    MatchChecker.h|44|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|


    And here is the code with string instead of char:
    Code:
    #include <stdio.h>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    #include <stack>
    
    // protections
    #ifndef MatchChecker_H
    #define MatchChecker_H
    
    using namespace std;
    
    class MatchChecker
    {
          public:
          static string matchChecker(string lineToCheck)
          {
                 stack<string> myStack;
    
                 for (int j = 0; j < lineToCheck.length(); j++) // line 20
                 {
                     string ch = lineToCheck[j];
                     switch (ch)
                     {
                            case '{':
                            case '[':
                            case '(':
                                 myStack.push(ch);
                                 break;
                            case '}':
                            case ']':
                            case ')':
                                 if (!myStack.empty())
                                 {
                                     string popper = myStack.pop();  // line 35
                                     if (((ch == '}') && popper == '{') || ((ch == ']') && (popper == '[')) || ((ch == ')') && (popper == '(')))
                                     {
                                        string returnValue = "Error: " + ch + " at the position " + j + " does not match";
                                        return returnValue;
                                     }
                                 }
                                 else
                                 {
                                     string returnValue = "Error: " + ch + " at the position " + j + " does not match";
                                     return returnValue;
                                 }
                                 break;
                            default:
                                 break;
                     }
                 }
                 if (!myStack.empty())
                 {
                 return ("Error: missing right delimiter");
                 }
          }
    };
    
    #endif

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    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:

    MatchChecker.h|21|warning: comparison between signed and unsigned integer expressions|
    MatchChecker.h|23|error: invalid conversion from `char' to `const char*'|
    MatchChecker.h|23|error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
    MatchChecker.h|24|error: switch quantity not an integer|
    MatchChecker.h|36|error: conversion from `void' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested|
    MatchChecker.h|39|error: no matching function for call to `strcmp(std::string&, char)'|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\string.h|43|note: candidates are: int strcmp(const char*, const char*)|
    MatchChecker.h|39|error: no matching function for call to `strcmp(std::string&, char)'|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\string.h|43|note: candidates are: int strcmp(const char*, const char*)|
    MatchChecker.h|39|error: no matching function for call to `strcmp(std::string&, char)'|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\string.h|43|note: candidates are: int strcmp(const char*, const char*)|
    MatchChecker.h|39|error: no matching function for call to `strcmp(std::string&, char)'|
    an so on....

    And then:
    MatchChecker.h|41|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|
    MatchChecker.h|47|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|



    Here is what I have right now:

    Code:
    #include <stdio.h>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    #include <stack>
    
    // protections
    #ifndef MatchChecker_H
    #define MatchChecker_H
    
    using namespace std;
    
    class MatchChecker
    {
    
          public:
          static string matchChecker(string lineToCheck)
          {
                 stack<string> myStack;
    
                 for (int j = 0; j < lineToCheck.length(); j++) // line 20
                 {
                     string ch = lineToCheck[j];
                     switch (ch)
                     {
                            case '{':
                            case '[':
                            case '(':
                                 myStack.push(ch);
                                 break;
                            case '}':
                            case ']':
                            case ')':
                                 if (!myStack.empty())
                                 {
                                    string popper = myStack.pop();
    
                                     //if (((ch == '}') && popper == '{') || ((ch == ']') && (popper == '[')) || ((ch == ')') && (popper == '(')))
                                     if (((strcmp (ch, '{') == 0) && (strcmp (popper, '}') == 0)) || ((strcmp (ch, '[') == 0) && (strcmp (popper, ']') == 0)) || ((strcmp (ch, '(') == 0) && (strcmp (popper, ')') == 0)))
                                        {
                                        string returnValue = "Error: " + ch + " at the position " + j + " does not match";
                                        return returnValue;
                                        }
                                 }
                                 else
                                 {
                                     string returnValue = "Error: " + ch + " at the position " + j + " does not match";
                                     return returnValue;
                                 }
                                 break;
                            default:
                                 break;
                     }
                 }
                 if (!myStack.empty())
                 {
                 return ("Error: missing right delimiter");
                 }
          }
    };
    
    #endif

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can switch on a single character, not a string. You can't use strcmp on a string, you use ==.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    Quote Originally Posted by tabstop View Post
    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.

    Thanks for all the help, tabstop!

    Here are the errors I am getting now:
    MatchChecker.h|21|warning: comparison between signed and unsigned integer expressions|
    MatchChecker.h|23|error: invalid conversion from `char' to `const char*'|
    MatchChecker.h|23|error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
    MatchChecker.h|37|error: conversion from `void' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested|
    MatchChecker.h|40|error: no match for 'operator==' in 'ch == '}''|
    MatchChecker.h|40|error: no match for 'operator==' in 'popper == '{''|
    MatchChecker.h|40|error: no match for 'operator==' in 'ch == ']''|
    MatchChecker.h|40|error: no match for 'operator==' in 'popper == '[''|
    MatchChecker.h|40|error: no match for 'operator==' in 'ch == ')''|
    MatchChecker.h|40|error: no match for 'operator==' in 'popper == '(''|
    MatchChecker.h|42|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|
    MatchChecker.h|48|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|


    And here is my code:

    Code:
    #include <stdio.h>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    #include <stack>
    
    // protections
    #ifndef MatchChecker_H
    #define MatchChecker_H
    
    using namespace std;
    
    class MatchChecker
    {
    
          public:
          static string matchChecker(string lineToCheck)
          {
                 stack<string> myStack;
    
                 for (int j = 0; j < lineToCheck.length(); j++) // line 20
                 {
                     string ch = lineToCheck[j];
                     char switchValue = lineToCheck[j];
                     switch (switchValue)
                     {
                            case '{':
                            case '[':
                            case '(':
                                 myStack.push(ch);
                                 break;
                            case '}':
                            case ']':
                            case ')':
                                 if (!myStack.empty())
                                 {
                                    string popper = myStack.pop();
    
                                     //if (((strcmp (ch, '{') == 0) && (strcmp (popper, '}') == 0)) || ((strcmp (ch, '[') == 0) && (strcmp (popper, ']') == 0)) || ((strcmp (ch, '(') == 0) && (strcmp (popper, ')') == 0)))
                                     if (((ch == '}') && popper == '{') || ((ch == ']') && (popper == '[')) || ((ch == ')') && (popper == '(')))
                                        {
                                        string returnValue = "Error: " + ch + " at the position " + j + " does not match";
                                        return returnValue;
                                        }
                                 }
                                 else
                                 {
                                     string returnValue = "Error: " + ch + " at the position " + j + " does not match";
                                     return returnValue;
                                 }
                                 break;
                            default:
                                 break;
                     }
                 }
                 if (!myStack.empty())
                 {
                 return ("Error: missing right delimiter");
                 }
          }
    };
    
    #endif

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    78
    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?

    Thank you for all of the help!!

    Here are the errors I am still getting:
    MatchChecker.h|21|warning: comparison between signed and unsigned integer expressions|
    MatchChecker.h|23|error: invalid conversion from `char' to `const char*'|
    MatchChecker.h|23|error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
    MatchChecker.h|37|error: conversion from `void' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested|
    MatchChecker.h|42|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|
    MatchChecker.h|48|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 01:10 AM
  3. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 09:37 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21