Sign in to follow this  
discodowney

strcmp

Recommended Posts

So i have an expression. Its just a simple bracketed addition sum. (4 + 2). The important parts of the code are below.

[code]


char *exp = "(4 + 2)";
int result = 0;

evaluate(exp, result);


bool evaluate(const char *expression, int &result)
{
while(expression[i])
{
if (!checkIfValid(expression[i]))
return false;

if(strcmp(&expression[i], "(")==0)
{
blah;
blah;
blah;
}

i++;

}
}

bool checkIfValid(const char expression)
{
if(isdigit(expression) || strcmp(&expression, "(")==0 || strcmp(&expression, ")")==0 ||
strcmp(&expression, "+")==0 || strcmp(&expression, "/")==0 || strcmp(&expression, "*")==0 ||
strcmp(&expression, "-")==0 || isspace(expression))
return true;
else
return false;
}


[/code]


the problem I'm having is that the checkIfValid method will return true when a bracket is put in. Thats fine, thats supposed to happen. But when i use the same code, if(strcmp(&expression[i], "(")==0), in the next if it fails. Can anyone see why this is? The only difference is the use of subscripting to say which char in the expression im comparing, but subscripting shouldnt be a problem should it?

Cheers

Share this post


Link to post
Share on other sites
strcmp() requires a '\0' terminated string. Since you are passing a char by value to checkIfValid, sending the address of this char to strcmp(), it will probably not find a '\0' right after the char in memory. Since you are comparing only to one character, why not just if (expression == '(' || expression == ')'), etc.

Edit:

Also, your strcmp(&expression[i], "(") isn't satisfied because strcmp is checking all chars from (expression + i) to the end of the string (the '\0'). again, since you are only checking one character, if (expression[i] == '(') should suffice.

Share this post


Link to post
Share on other sites
[quote name='discodowney' timestamp='1318429731' post='4871845']
[code]
bool checkIfValid(const char expression)
{
if(isdigit(expression) || strcmp(&expression, "(")==0 || strcmp(&expression, ")")==0 ||
strcmp(&expression, "+")==0 || strcmp(&expression, "/")==0 || strcmp(&expression, "*")==0 ||
strcmp(&expression, "-")==0 || isspace(expression))
return true;
else
return false;
}
[/code]
[/quote]
If you're only wanting to compare a single char, as appears to be the case in all these strcmp() calls, just use the built-in equality operator for char:

[code]
bool checkIfValid(char ch)
{
return isdigit(ch) || isspace(ch) || ch == '(' || ch== ')' || ch == '+' || ch == '/' || ch == '*' || ch == '-';
}
[/code]

Or:

[code]
bool checkIfValid(char ch)
{
return isdigit(ch) || isspace(ch) || (ch != '\0' && strchr("()+/*-", ch));
}
[/code]

Though I also tend to agree with ApochPiQ's comment about std::string usage.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this