0

I am trying to compare strings in cpp but i got a bunch of errors like
no match for 'operator=='
invalid conversion from char to const char *

I'm implementing two functions :
1) compare the strings and search for brackets, then return a bool.
2) compare the strings and search for arithmetic operators, then return a bool.

Here's my code :

bool isBracket(const string b)
{
    if(b == ")" || b=="(")
            return true;
    else
            return false;
}

bool isOperator(const string op)
{
    string ops= "*+-^/";
    for(int i = 0; i < ops.length(); i++)
    {
            if (op == ops[i])
                    return true;
            else 
                    return false;
    }
}

int main()
{
    string exp="(a+b)";

    for(int i=0; i < exp.size(); i++)
    {
        cout<<exp[i]<<endl;
        if(isBracket(exp[i]))
                cout<<"bracket found"<<endl;
        if(isOperator(exp[i]))
                cout<<"operator found"<<endl;
    }
    return 0;
}
4
  • Have a look stackoverflow.com/questions/9158894/… Commented Sep 21, 2014 at 9:09
  • Please post the complete and unedited error log, and also point out where in the source you get the errors. Commented Sep 21, 2014 at 9:09
  • Also, your isBracket function can be simplified to return (b == "(" || b == ")"); Commented Sep 21, 2014 at 9:11
  • 2
    ops[i] is a char. There's no comparison between std::string and a single char. Commented Sep 21, 2014 at 9:12

3 Answers 3

4

The functions could look the following way

bool isBracket( const string &b )
{
    return b.find_first_of( "()" ) != std::string::npos;
}

bool isOperator( const string &op )
{
    return op.find_first_of( "*+-^/" ) != std::string::npos;
}

Or if you are comparing only elements of a string then the functions could look as

#include <cstring>
//...

bool isBracket( char b )
{
            const char *brackets = "()"; 
            return std::strchr( brackets, b ) != NULL; 

}

bool isOperator( char op )
{
            const char *ops = "*+-^/";
            return std::strchr( ops, op ) != NULL; 
}

Or even like

#include <cstring>
//...

bool isBracket( char b )
{
            const char *brackets = "()"; 
            return b != '\0' && std::strchr( brackets, b ) != NULL; 

}

bool isOperator( char op )
{
            const char *ops = "*+-^/";
            return op != '\0' && std::strchr( ops, op ) != NULL; 
}
Sign up to request clarification or add additional context in comments.

Comments

2

The comparison op == ops[i]

compares a std::string and a char, if op is a one-char string you could do op[0] == ops[i] instead.

Also, I suggest you look at a good reference book, e.g. Josuttis "The C++ Standard Library: A Tutorial and Reference". There are also decent online references, just Google for them. There you can find a lot of useful string functions, and STL algorithms may also be useful, as other posters have already pointed out.

Comments

0

Your isOperator() function would always return false, because (as mentioned) you try to compare a string with a char. on the other hand, it wouldn't even work if you would compare a char with a char, but you'd have to try out every single char in the op string.

AND you'd have to change the return statement after the if(). instead, return false only if it went through the whole for loop.

someone else did post some even more useful code, but another question from me, wouldn't it be more useful to return where the operator (and maybe even which operator) has been found?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.