Thread: Problem with my output

  1. #1
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72

    Problem with my output

    Okay. So I have started a new program that is meant to query the user to input a number greater than 2. The program then processes that numbers divisors and prints them out on the screen. Eventually, I want the program to also print the sum of those integers to show if the
    number is a perfect number or not.

    The problem I am having right now is with my integers. My program successfully ( and correctly ) prints out my divisors as long as the inputted integer is 30 or less. Anything higher prints 1 and 2. What am I not seeing or what did I do wrong? I'm not worried about the second half of my program yet ( as you can see, I am not there yet in my code). I want to make sure the functionality works before playing with the rest of my input.

    Thanks.

    Code:
     
    
    /*write a program which accepts as input an integer (which must be 2 or greater)*/
    /*and prints out its proper divisors in ascending order, and to print the sum of its */
    /*proper divisors.  The program should also report whether the input number is perfect or not.*/
     
    #include <stdio.h>
     
    int main ( void )
    {
         int n, d = 1;
         
         printf("Enter a number greater than two:\t");
         scanf("&#37;d", &n);
         getchar ();
         
         printf("The proper divisor(s) of %d is:\n", n);
         
         while ( d < n ){
               if ( n % d )
                   d++;
               else
                   printf("%d\t", d);
                   d++;
         }
         
         getchar();
         return 0;
                       
    }
    Last edited by Dogmasur; 08-04-2008 at 07:10 PM.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    You're missing some braces so the second d++ is always executed.

    Code:
    while ( d < n )
    {
        if ( n % d )
            d++;
    
        else
        {
            printf("%d\t", d);
            d++;
        }
    }
    It is for this reason that I always use braces with things like if statements and loops, even if there is only one line to be executed.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Or you could write:

    Code:
              
    if ( n % d == 0 )
          printf("%d\t", d);
    d++;

  4. #4
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Yep, that did it. Thanks. There is a good lesson in paying close attention to syntax.

    So, if I wanted to take all the divisors and add them together, would I be best to create a function to store them into an array and then to return the sum of that array when it is complete or could I code something like that within main and not bother with a function? Either way, is there a way I can store them as they are discovered, so the program does not have to go through and get them later on?

  5. #5
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by noops View Post
    Or you could write:

    Code:
              
    if ( n % d == 0 )
          printf("%d\t", d);
    d++;

    So the the if statement checks the truth of n%d being 0 and ignores anything else and then goes back to the loop for the next number? I haven't used '=='.

  6. #6
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Dogmasur View Post
    So the the if statement checks the truth of n&#37;d being 0 and ignores anything else and then goes back to the loop for the next number? I haven't used '=='.
    Yup. And for the sum you could use another variable to keep a running total. Whenever you find that n % d is 0 just add d to that running total variable. You will need brackets with the if statement if you add this.

    edit: Also, '==' means 'equal to', and '=' means you are assigning a variable. be careful not to mix them up.

  7. #7
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Code:
    while ( d < n ) {
         if ( n % d  == 0 )
             printf("%d\t", d)
             d++;
    }
    Where should the enclosing braces go for adding my variable to keep track of the sum? I am using s as the variable, initialized it to 0, and my expression will read s = d + s.

  8. #8
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    You use the braces for 'if' just like you do for 'while'. So if(){stuff} will execute 'stuff' when the if is true.
    Code:
    if(condition)
    {
         stuff;
         morestuff;
    }
    edit: also, you could look into using a 'for' loop for this.

  9. #9
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    I had my 'd++' inside the if braces and it was affecting my output. Now it's fixed. Thanks.

  10. #10
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Here is the final code for my program, which seems to work fine.

    Code:
    /*write a program which accepts as input an integer (which must be 2 or greater)
     and prints out its proper divisors in ascending order, and the sum of its pro-
     per divisors.  The program should also report whether the input number is per-
     fect or not.*/
     
    #include <stdio.h>
     
    int main ( void )
    {
         int n, d = 1, s = 0;
         
         printf("Enter a number greater than two:\t");
         scanf("%d", &n);
         getchar ();
         
         printf("The proper divisor(s) of %d is:\n", n);
         
         while ( d < n ){
               if ( n % d == 0 ){
                    s = d + s;
                    printf("%d\t", d);
                    }     
                    d++;
         }
         
         printf("\nThe sum of the divisors is %d.\n", s);
         
         if ( n == s )
             printf("%d is a perfect number.\n", n);
         else
             printf("%d is not a perfect number.\n", n);
         
         getchar();
         return 0;
                       
    }

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You need to fix your indentation on that if statement inside the while loop. Also the other if/else statement is still missing brackets. Your code was a prime example of why you should always put brackets with every if/else/while/for/do statements.
    Code:
    while ( d < n ){
        if ( n &#37; d == 0 ){
            s += d;
            printf("%d\t", d);
        }     
        d++;
    }
    That's what your indentation should look like. Indentation is there to give you an idea of the logical grouping of statements and of scope.

    QuantumPete
    Edit: Also 'n' is not initialised!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    I see what you mean about the proper indentation. I didn't think I needed to initialize variable 'n' because it user-defined and is not used for any operations until the user has defined its value.

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Dogmasur View Post
    I see what you mean about the proper indentation. I didn't think I needed to initialize variable 'n' because it user-defined and is not used for any operations until the user has defined its value.
    At the moment, yes. But imagine some time in the future, when your code is 10,000 lines long. You need to add a statement that uses n. You'd have to trace through all the code to find out when/where n is initialised (and no, "I'll know when it is initialised" is not a valid argument. You'd be surprised how much you forget about code in a matter of weeks).
    Better to init the variable at creation. Then you can be assured that it has a valid value anytime that it is in scope.
    The thing with variable initialisation, brackets and braces, putting constants on the lhs for comparisons etc, is that while you may not see any benefit in it now, when you come back to alter or simply read the program later, there'll be fewer surprises and thereby fewer bugs.
    Just because you can get away with something in C doesn't mean it's good practice or even safe.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Good points. I understand what you're saying. Thanks.

  15. #15
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    printf("Enter a number greater than two:\t");
         scanf("&#37;d", &n);
         getchar ();
    Perhaps, you should write a proper function to clear off the input buffer, but not just using the getchar function to clear it. Since there might be more than one char until you reach EOF char, which needs to be cleared. Use the following function to clear the input buffer.

    Code:
    void Clear_Buffer( void )
    {
         int ch;
         while( ( ch = getchar() ) != '\n' && ch != EOF );
    }
    And, try avoiding using scanf function.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hash Table Problem.
    By penance in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 01:03 PM
  2. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 06:42 PM
  3. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 07:36 AM
  4. Problem with output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 08:32 PM