Thread: Why doesn't this c program work?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    Why doesn't this c program work?

    Code:
     
     * This program is designed to print a Fortune Cookie
     * message for a positive integer entered by the user.
     
    #include <stdio.h>
    
    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

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    The problem is about the printf (they do not show up)?

    If you put a printf before you call the print function you will see the number of "sum"....try it, and you will see that it is not 1 or 2 or 3 or...or 9!!! So your mistake is before this...
    Last edited by brack; 09-03-2010 at 08:15 AM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    you haven't initialized sum_int here...

    Code:
    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;
    
                    /* ... */
    should have been...

    Code:
    int sum_digits(int number)
    {
    	int remainder, sum_int = 0;
    
    	while(number!=0 && remainder!=0)
    	{
    		sum_int= sum_int+ number- number/10*10;
    		number= number/10;
    
                    /* ... */
    there's a better way to sum up the individual digits of a no. as...

    Code:
    while (number > 9)
        number = number % 9;
    
    if (number == 0)
        return (9);
    return number;

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Code:
    while (number > 9)
        number = number % 9;
    
    if (number == 0)
        return (9);
    return number;
    [/QUOTE]


    Thanks it works much better!

    Any reason behind why number%9 would be the same as adding each individual numer?

    Hey branc, thanks! I forgot about using printf as a debugging tool!

    Anyone can point out why my previous code won't work as compared to the method xabhi proposed which works? Any logic errors cause mine can be compiled but whenever i key in a integer, the program just waits and like go on forever without an answer?

    Code:
    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;
    }
    Last edited by hobolux; 09-03-2010 at 09:03 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    printf is a poor debugging tool. I suggest you get familiar with a real debugger instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    it is poor as you say(i agree) but does its job well...
    for me at least...
    and for this program for sure...

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Quote Originally Posted by Elysia View Post
    printf is a poor debugging tool. I suggest you get familiar with a real debugger instead.
    ok i just started learning C and I only used the compiler gcc in ssh secure shell client to see the errors to debug or use the printf statement.
    Last edited by hobolux; 09-03-2010 at 09:36 AM.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by hobolux View Post
    ...

    Anyone can point out why my previous code won't work as compared to the method xabhi proposed which works? Any logic errors cause mine can be compiled but whenever i key in a integer, the program just waits and like go on forever without an answer?
    If you want ideas on "why my previous code won't work", please write a one sentence purpose statement for the function that is currently called sum_digits( ) so we know what "will work" is supposed to be. That sentence should be the comment (at a minimum) before your function definition. Once you have written the purpose statement, you may want to change the name the function to better describe it.

    Is the real purpose "Return an integer from 1 to 9 based on the number in a way that is not obvious."? If so, the function might be better named pick_a_number_between_1_and_9( ) or just pick_a_number( ).

    My first reading of your code would suggest that currently you are returning 0 for all possible input values. I could very easily be wrong.

    Edit: My second reading is that this function has two uninitialized automatic variables. One of the revisions in the thread initializes one of the them. So the function has undefined behavior.
    Last edited by pheininger; 09-03-2010 at 10:04 AM.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Code:
    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;
    }
    The purpose of the functiom is to add up the digits of the number which the user has entered repeatedly until the sum is a single digit. For example, if the user key in number 123456, then adding the digits would give 1+2+3+4+5+6=21, then adding the digits again would give 2+1=3!!

    For the conditions of the while loop i make it such that it will only stop when the number=0 and the remainder=0 which implies the sum is a one digit number already.

    Inside the while loop, i make it such that the sum= intial sum(which should be 0 at the first cycle)+ number -number/10*10 (Why did i divide it and multiply it? since number is a int defined by me, so dividing it and multiplying it would give me the number without the ones digit e.g. 198/10*10=19*10=190)

    then i use number= number/10 to get rid of the digit in the ones place which i added to the sum already.

    then i use remainder = sum%10 to see if sum is one digit

    The if loop is used for the case that number reaches 0 but sum is a double or more digit number so i make number=sum and initialise sum so the cycle repeates!

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    then i use remainder = sum%10 to see if sum is one digit
    That won't tell you if the sum is one digit. Look at it this way:

    5 % 10 = 5
    15 % 10 = 5
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by brack View Post
    it is poor as you say(i agree) but does its job well...
    for me at least...
    and for this program for sure...
    Quote Originally Posted by hobolux View Post
    ok i just started learning C and I only used the compiler gcc in ssh secure shell client to see the errors to debug or use the printf statement.
    printf does not do a well job. It is, like I said, a poor debugging tool.
    I suggest you instead read a basic debugging tutorial and you'll find how much easier, faster and powerful you can make your debugging compared to your naive printf statements. And I repeat: you only need to know the basics of using a debugging to surpass the printf statement for debugging. That is how poor printf really is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Quote Originally Posted by bithub View Post
    That won't tell you if the sum is one digit. Look at it this way:

    5 % 10 = 5
    15 % 10 = 5
    oh no. no wonder! i thought 5%10=0!

    Thanks a lot.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Quote Originally Posted by Elysia View Post
    printf does not do a well job. It is, like I said, a poor debugging tool.
    I suggest you instead read a basic debugging tutorial and you'll find how much easier, faster and powerful you can make your debugging compared to your naive printf statements. And I repeat: you only need to know the basics of using a debugging to surpass the printf statement for debugging. That is how poor printf really is.
    Ok. I will keep that in mind.

  14. #14
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    You realize that
    Code:
    number % 10 == number - number/10*10
    is always true for positive numbers. It may be true for negative numbers, but I always have to remind myself what happens with integer division and remainder on negative numbers.
    Last edited by pheininger; 09-03-2010 at 04:17 PM.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by Elysia View Post
    printf does not do a well job. It is, like I said, a poor debugging tool.
    I suggest you instead read a basic debugging tutorial and you'll find how much easier, faster and powerful you can make your debugging compared to your naive printf statements. And I repeat: you only need to know the basics of using a debugging to surpass the printf statement for debugging. That is how poor printf really is.
    You somehow persuade me to leave printf as a "debugger", so if you could tell me how to start with a debugger i would be glad full!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my program doesnt work
    By eagle0eyes in forum C Programming
    Replies: 7
    Last Post: 05-31-2010, 12:25 PM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Can not get this program to work?
    By BLG in forum C Programming
    Replies: 9
    Last Post: 09-09-2009, 09:28 AM
  4. threaded program doesn't work?
    By OcTO_VIII in forum Linux Programming
    Replies: 1
    Last Post: 12-11-2003, 12:37 PM
  5. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM