cbes cbes - 19 days ago 8
C Question

Loop counter prints same value each time with different input

So this loop is supposed to count the number of numerical chars in a line, but it prints the same value each time (6356732). What am I doing wrong?

if((i >= '0') && (i <= '9'))
{
printf("%c\n", i);
count = count++;
}


count is just declared 'int count = 0;'.

edit; I made a change suggested below but the output hasn't changed?

while(fscanf(f, "%c\n", &i) !=EOF)
{
if((i >= '0') && (i <= '9'))
{
count = 0;
sum = 0;
printf("%c\n", i);
count++;
sum++;
}
}


edit 2; OK so I've got the program working as intended with the help of all you lovely folk! Thankyou very much!

while(fscanf(f, "%c\n", &i) !=EOF)
{
if((i >= '0') && (i <= '9'))
{
printf("%c\n", i);
count++;

}
}

count is defined at zero at the start of the program.

Answer

You should just use count++; or count = count + 1;, because count = count++; is not defined. You can read more about the subject in this post.