strcmp

Started by
4 comments, last by Concentrate 12 years, 6 months ago
So i have an expression. Its just a simple bracketed addition sum. (4 + 2). The important parts of the code are below.




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

evaluate(exp, result);


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

if(strcmp(&expression, "(")==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;
}





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, "(")==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
Advertisement
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, "(") 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 == '(') should suffice.
Sound man, Thanks. I've not used strcmp much at all, so wasnt sure how to go about this.
Since you appear to be using C++, the correct thing to do is use std::string for handling anything longer than a single char.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]



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;
}


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:


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


Or:


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


Though I also tend to agree with ApochPiQ's comment about std::string usage.
Maye...
bool isValid(char ch){
const std::string validChars = "()+/*-";
return isdigit(ch) || isspace(ch) || validChar.find(ch) != string::npos;
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

This topic is closed to new replies.

Advertisement