Jump to content
  • Advertisement
Sign in to follow this  
discodowney

strcmp

This topic is 2564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Since you appear to be using C++, the correct thing to do is use std::string for handling anything longer than a single char.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites
Maye...
bool isValid(char ch){
const std::string validChars = "()+/*-";
return isdigit(ch) || isspace(ch) || validChar.find(ch) != string::npos;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!