newbie help

This is a discussion on newbie help within the C Programming forums, part of the General Programming Boards category; Just started C programming, and I'm trying to make this little code. I'm guessing you guys know what its point ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    3

    newbie help

    Just started C programming, and I'm trying to make this little code. I'm guessing you guys know what its point is since ur all expererts:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int input, x, count;
    	printf("Enter a positive integer: ");
    	scanf("%d", &input);
    	printf("The factors of %d are: \n", input);
    	for (x = 1, count = 0; x <= input; x++)  	
    		{if (input % x == 0)		
    		printf("%d is a factor of %d\n", x, input);
    		count++;
    		}
    	printf("# of factors is %d\n", count);	
    }
    The problem is when I run this the "count" vcariable always equals "input". Why is it so, I mean I did the if statement so that the count increases only if input % x == 0. A bit confusing . Thanks in advance
    Last edited by winnerpl; 10-10-2004 at 07:32 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > I'm guessing you guys know what its point is since ur all expererts:[code]
    Ohh, so close with the code tags - but no cigar today
    Press edit and try again
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The problem is when I run this the "count" vcariable always equals "input".
    Of course it does, the loop iterates input times, and because count starts out at 0, it will be equal to input - 1 on the last iteration and input after the loop ends. Walk through the code, it makes sense.

    >I mean I did the if statement so that the count increases only if input % x == 0.
    Ah, but you didn't. If an if statement body is not surrounded by braces then only the next statement will be part of the body. Properly indented, your loop looks like this:
    Code:
    for (x = 1, count = 0; x <= input; x++) 
    {
      if (input % x == 0) 
        printf("%d is a factor of %d\n", x, input);
      count++;
    }
    If you only want count to be incremented if input is evenly divisible by x then you need braces:
    Code:
    for (x = 1, count = 0; x <= input; x++) 
    {
      if (input % x == 0) {
        printf("%d is a factor of %d\n", x, input);
        count++;
      }
    }
    And don't forget return 0 at the end of main, that's important.

    >I'm guessing you guys know what its point is since ur all expererts
    What I wouldn't give to be able to understand code at a glance. Knowing what each piece does isn't the same as understanding how they work together as a whole. The former is easy, the latter takes a bit of staring even for simple programs.
    My best code is written with the delete key.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by Prelude
    >
    And don't forget return 0 at the end of main, that's important..
    I've read smewhere something like this:
    "
    Finally, the ANSI/ISO C++ Standard states that a

    return 0;

    statement implicitly is understood to come at the end of the main() function (but of no other function) if you don't explicitly provide it. "

    Does that means that C standard require return 0; and why is that important ?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >statement implicitly is understood to come at the end of the main() function
    This is only true of the C99 standard, which is not widely implemented enough to assume at this point. Therefore, we assume C89, where omitting a return value is undefined.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    Thanks guys for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21