I just can't seem to figure this out. My program is: Put unlimited characters until you put " ! ". If there are numbers in those characters, calculate their sum.
Code:
#include <stdio.h>
int main()
{
char character;
int digit = 0,sum=0;
while(1){
digit = 0;
scanf("%c", &character);
if(character=='!')
break; // Put unlimited characters until '!'
if(character>='0' && character<='9') // Check if the character is a digit
{
digit=(int)character; // Store it
}
sum+=digit;
}
printf("%d", sum);
return 0;
}
In the code above, i always get sum = 0. What is the problem?