0

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.
  • 1
    Can you paste the whole loop? In this snippet `i` isn't changing at all; so it's not surprising that it's printing the same value. Did you maybe mean to printf `count`? – Alejandro C. Nov 21 '16 at 19:53
  • @cbes You are initializing `count` to `0` each time you find a digit. You only need to initialize it once. Besides I don't see `i` is defined. You are reading in `f` but comparing with `i`? – P.P Nov 21 '16 at 20:02
  • where do you get 6356732? Nothing in yr code prints the count (which will always be zero in what you show) – pm100 Nov 21 '16 at 20:05
  • Yeah I've removed the count=0. However, count is still ending up with a value of '6356732'. Count is being printed outside the loop, further down in main(). Also the values of i are saved in a text file earlier in the process, this loop is reading from the file and printing the results to the console. –  Nov 21 '16 at 20:06
  • `count = 0` should go before the `while` loop. Is your `printf` producing the characters you expect to see? – yano Nov 21 '16 at 20:13
  • @cbes I can't see how exactly you read from the file and count. I have updated my answer with an example that should help to see the problem in your code and you can modify it to suit your needs. – P.P Nov 21 '16 at 20:14
  • printf seems to be working perfectly. I'll attach an image of the [output](http://i.imgur.com/GVF5YWf.png) –  Nov 21 '16 at 20:18
  • add the code that prints out `count` to your question – yano Nov 21 '16 at 20:26

2 Answers2

3
count = count++;        

is undefined behaviour. For your purpose, you just need:

count++;

Here's a simple example to read a line from stdin and count chars (which you can easily modify to suit your file reading and counting):

#include <stdio.h>

int main(void) 
{   
    char line[256];
    int count = 0;
    int sum = 0;
    size_t i = 0;

    if (fgets(line, sizeof line, stdin)) {
       while(line[i]) {
          if((line[i] >= '0') && (line[i] <= '9')) { 
             printf("%c\n", line[i]); 
             count++; 
             sum = sum + line[i] - '0';
          }
          i++;
       }  
    }

    printf("%d %d\n", sum, count);
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
P.P
  • 117,907
  • 20
  • 175
  • 238
2

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.

Community
  • 1
  • 1
Honza Dejdar
  • 947
  • 7
  • 19