help - how to find int from keyboard

// input >> abcd234. output>> total= 9


#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
char c;

c='a';
int sum=0;

while(c!='.'){
scanf("%c",&c);
if (isdigit(c))
sum+=c;
}
printf("%d",sum);

system("PAUSE");
return 0;
}
Chars work in ascii values. so 'a' is 97. '0' is 48. so you have to subtract '0' from c to get the actual number value :P

1
2
3
if (isdigit(c))
sum+=(c-'0');
}

haven't tested will it work :/
thank you :P
Topic archived. No new replies allowed.