Thread: Please Help !

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63

    Please Help !

    Hi,
    I am pretty new in c programming, I started wrting codes but am having trouble finding the error in this one. can any help me

    Code:
    #include <stdio.h>
    
    void numberof(int);
    
    int main(void)
    {
    int num;
    	printf("Enter the number:");
    	scanf("%d",&num);
    if (num>=1000000 || num <=9999)
    	printf("Too big");
    else 
     numberof(num);
    }
    
    void numberof(int number)
    {
    int i;
    int digits; // number of sevens
    int result =(number%10)/7;
    
    for(i =1; i <6;i++)
    {
    	if(result == 1)
    		digits++;
    	number/10;
    }
    	printf("%d, digits", digits);
    
    }
    The problem is to find the number of sevens in the user inputed 5-digit number.

    But the problem in the output is that

    if the user types any number with 7 in it..that is 28517 or 27752 ..its always showing as 5 sevens.

    I am having trouble in finding the problem . Any help is appreciated. Thanking in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Look at your loop -- how does result change inside the loop? Don't you think maybe it should?

    Also, digits starts out as some random number, which apparently is 5 but could just as easily be 11 or 1234123.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Why aren't you using the formula (number%10)/7 inside the for loop?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by itCbitC View Post
    Why aren't you using the formula (number%10)/7 inside the for loop?
    The better question is why he is using that formula in the first place.

    Obelisk, I'll give you a little hint:
    Code:
    if((number % 10) == 7)
        printf("The least significant digit is a 7\n");
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by Obelisk View Post
    Hi,
    I am pretty new in c programming, I started wrting codes but am having trouble finding the error in this one. can any help me

    Code:
    #include <stdio.h>
    
    void numberof(int);
    
    int main(void)
    {
    int num;
    	printf("Enter the number:");
    	scanf("%d",&num);
    if (num>=1000000 || num <=9999)
    	printf("Too big");
    else 
     numberof(num);
    }
    
    void numberof(int number)
    {
    int i;
    int digits; // number of sevens
    int result =(number%10)/7;
    
    for(i =1; i <6;i++)
    {
    	if(result == 1)
    		digits++;
    	number/10;
    }
    	printf("%d, digits", digits);
    
    }
    The problem is to find the number of sevens in the user inputed 5-digit number.

    But the problem in the output is that

    if the user types any number with 7 in it..that is 28517 or 27752 ..its always showing as 5 sevens.

    I am having trouble in finding the problem . Any help is appreciated. Thanking in advance
    First of all thnks for the quick replies... Yes its a better idea of puting the forumla inside the loop..which i also figured ..


    My logic was

    Code:
     int result =(number%10)/7;
    its to test test the last digit of the number for 7

    then to test the next digit, i used number/10...since both are intgers i would delete the last digit by "/" function

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by bithub View Post
    The better question is why he is using that formula in the first place.

    Obelisk, I'll give you a little hint:
    Code:
    if((number % 10) == 7)
        printf("The least significant digit is a 7\n");
    Yep! that is a better formula since it shaves off a computational step.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Thnx guys...for the help.. my mistake it seems the result didnt update itself

    instead of
    Code:
     number/10
    i had to do

    Code:
     number = number/10
    thanx agn

  8. #8
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Hey guys if u dnt mind...can u tell me wether there is any better c compiler than cygwin..bcoz its kinda hard to debug. ( got used to eclipse in java)

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by itCbitC View Post
    Yep! that is a better formula since it shaves off a computational step.
    The problem isn't that it shaves off a computational step. The problem is that code will return 1 if the last digit is a 7, 8, or 9...
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Obelisk View Post
    Hey guys if u dnt mind...can u tell me wether there is any better c compiler than cygwin..bcoz its kinda hard to debug. ( got used to eclipse in java)
    If you are looking for an IDE (like eclipse), check out code blocks. I don't know if it comes with a debugger though (I don't do much Windows programming anymore).

    You can also check out MS Visual Studio Express which is free to download.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    ok will check those out..it seems that code blocks has debugging in it

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by bithub View Post
    The problem isn't that it shaves off a computational step. The problem is that code will return 1 if the last digit is a 7, 8, or 9...
    Argh! for some moronic reason the formula in my head was (number % 10) % 7 (brain fart!).

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by itCbitC View Post
    Argh! for some moronic reason the formula in my head was (number % 10) % 7 (brain fart!).
    That wouldn't work either. (number % 10) % 7 will check if the last digit is an 8 -- not a 7
    bit∙hub [bit-huhb] n. A source and destination for information.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by bithub View Post
    That wouldn't work either. (number % 10) % 7 will check if the last digit is an 8 -- not a 7
    Yes it would
    Code:
    if (((number % 10) % 7) == 0)
        ...

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by itCbitC View Post
    Yes it would
    Code:
    if (((number % 10) % 7) == 0)
        ...
    Uh yeah, if you completely change the statement from an integer assignment to an comparison to zero.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed