This is a simplified version of my code:
void calc(char *s)
{
int t = 0;
while (*s)
{
if (isdigit(*s))
t += *s - '0';
else
++s;
}
printf("t = %d\n", t);
}
int main(int argc, char* argv[])
{
calc("8+9-10+11");
return 0;
}
The problem is with the while loop running forever, though I’m expecting it to stop after the final digit 1. And my expected output is t = 20.
sis not incremented if*sis a digit, consider removing the else clause, making the code into this: