Thread: sum is always printed as 0

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    2

    sum is always printed as 0

    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?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    I don't know why it's printing 0 for you. The way it's written it should be giving you the sum of the ascii values of all the digit characters it's seen before the bang. That's what it does for me. The input 0123456789! prints 525, the sum of 48, 49, ..., 57, the ascii values of '0', '1', ..., '9'.

    Remember that the numeric value of, e.g., '7' is '7' - '0'. Also note that to read a multi-digit number (if that's your purpose) requires you to accumulate that value for each digit, multiplying the old value by 10 and adding in the newest digit. Then when you encounter a non-digit and the accumulated value is non-zero, add it into sum and reset it to zero for the next number.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,707
    Clearer indentation would help.
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does every character get printed twice?
    By nerio in forum C Programming
    Replies: 2
    Last Post: 01-25-2016, 06:44 AM
  2. Why Garbage value is getting printed???
    By Siddarth777 in forum C Programming
    Replies: 3
    Last Post: 08-16-2012, 11:12 AM
  3. array being printed incorrectly
    By amissot in forum C Programming
    Replies: 4
    Last Post: 12-29-2011, 05:10 AM
  4. Getting a bmp copy of what i printed
    By knutso in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2007, 05:29 AM
  5. int wrong value being printed?
    By Axel in forum C Programming
    Replies: 11
    Last Post: 10-14-2006, 01:37 AM

Tags for this Thread