Why doesn't this c program work?

Discussion in 'C' started by hobolux, Sep 3, 2010.

  1. hobolux

    hobolux New Member

    Joined:
    Sep 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    This program is designed to print a Fortune Cookie message for a positive integer entered by the user.
    Code:
    int sum_digits(int);
    void print_cookie(int);
    
    int main(void)
    {
        int number, // input
                    sum;        // the sum of the digits for the number
    
        printf("Enter a positive integer: ");
        scanf("%d", &number);
        
            sum= sum_digits(number);
    
            print_cookie(sum);
    
        return 0;
    }
    
    int sum_digits(int number)
    {
            int remainder, sum_int;
    
            while(number!=0 && remainder!=0)
            {
                    sum_int= sum_int+ number- number/10*10;
                    number= number/10;
                    remainder=sum_int%10;
    
                    if(number==0 && remainder!=0)
                    {
                            number=sum_int;
                            sum_int=0;
                    }
            }
            return sum_int;
    }
    
    void print_cookie(int sum)
    {
            switch(sum)
            {
                    case 1:
                    printf("You will have a fine capacity for the enjoyment of life.\n");
                    break;
                    case 2:
                            printf("Now is the time to try something new.\n");
                    break;
                    case 3:
                            printf("Don't let doubt and suspicion bar your progress.\n");
                    break;
                    case 4:
                    printf("Your principles mean more to you than any money or success.\n");
                    break;
                    case 5:
                    printf("Accept the next proposition you hear.\n");
                    break;
                    case 6:
                    printf("A handful of patience is worth more than a bushel of brains.\n");
                    break;
                    case 7:
                    printf("You have an active mind and a keen imagination.\n");
                    break;
                    case 8:
                    printf("You are talented in many ways.\n");
                    break;
                    case 9:
                    printf("Treat everyone as a friend.\n");
                            break;
            }
    }
    
    The function of the function sum_digits is to calculate the sum of the digits of the input number and the sum must be a single digit since the fortune numbers are single digited so if the sum exceeds one digit, add them together

    e.g.
    input number is 12345
    sum= 1+2+3+4+5
    sum= 15
    sum= 1+5
    sum= 6
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice