Reputation: 1
So I've got an assignment in school, to building a calculator. The program should consist of a "parcer.c"-file where all the not allowed inputs are sorted out, and if any unallowed values and inputs are submitted it should give you back a "Syntax Error" -message.
i've got it done, only one thing left, and it's this, a if that's supposed to check and make sure that the sizeof (op) is not bigger than 1, meaning that it can only consist of 1 sign(+,-,,/,%) But my problem is that i still can write; 4 +sdf 5 and get the result = 9 But i cant write 4sdf 5 or 4 sdf 5, becuse then I get my "Syntax Error", which is correct.
What should i turn around to give me the same error when I've got junk after the operator? EX: 4 +sdf 5
if(sizeof (*op) > 1 || !validOperator(op))
{
//Returns statuscode 2 and a "Syntax Error" if the operator
is consisting of more than 1 sign
printf("\n Syntax Error, only +, -, x, /, %% are allowed\n");
return 2;
}
Upvotes: 0
Views: 62
Reputation: 353
I assume op is defined as
char *op; /*or maybe as char op[]*/
When you check with sizeof(*op) you check the size of the first element in the array of chars, i.e. it will always return 1.
You want to use strlen() instead of sizeof(). Try following change:
if( op!=0 && (strlen(*op) > 1 || !validOperator(op)) )
Upvotes: 1