c - Need help figuring out infinite loop -


this simplified version of 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 while loop running forever, though i'm expecting stop after final digit 1. , expected output t = 20.

s not incremented if *s digit, consider removing else clause, making code this:

while (*s) {     if (isdigit(*s))         t += *s - '0';      ++s; } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -