I'm currently trying to create a Reverse-Polish notation interpreter in C, using a stack implemented using struct
. It should take all single-digit values (0-9) and the operators +
,-
,*
and /
, and reject all others by exiting the program.
I am trying to capture the whole expression as it is typed in as a string, which has the type char, however when I use isdigit()
it always returns a non-zero function (IE, it is not a digit) even when it appears to be to the user. I believe this is to do with the fact that the string is of type char, but I don't think I can use anything else, else I would get error messages when entering the operators.
The error is as follows: suppose I enter "11+"
into the function. In the debug, I can see this appear in the watches. Advancing the program, I see that isdigit ()
has returned 1 instead of 0, so the if statement conditions are fulfilled, and the program exits with exit(1); There is no specific error message provided by the IDE.
Is there a way to implement a conversion of just the "digits" of the string to type int, or do I have to do something else?
Here is the function. although it is still raw and unfinished, this shows the error:
void parseRPN(TopStack *st)
{
char Input[50];
int i;
do{
printf("please enter an expression in single-digit integers"
"using Reverse Polish notation:");
scanf("%s",&Input);
if (sizeof(Input)/sizeof(int)>=50)
{
printf("that expression was too large for the RPN engine to handle!"
"please break it down into smaller sub-tasks.\n");
fflush(stdin);
continue;
}
break;
}while(true);
for (i=0;i<50;i++)
{
int ErrorDetect=isdigit(Input[i]);
if (ErrorDetect==0 && (Input[i]) != '+' || '-' || '*' || '/')
{
printf("Error: Invalid operand to RPN\nExiting...");
exit(1);
}
else printf("great success!");
}
}