Thread: Problem with Program to Check if Separators Match

Hybrid View

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

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

  3. #3
    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'|

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, 02:10 AM
  3. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 10:37 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM