Thread: trying something from a book is not working and not understanding why

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    trying something from a book is not working and not understanding why

    Hi, I am testing a below from book " C : a modern approach"..

    I don't understand why this program is not working ... when i put 123, it digit_seen should still be false(intialized all w/ 0).. but it goes into true and breaks the loop.

    Can someone see why it doesn't work?

    Code:
    /* Checking a number for repeated digits */
    
    #include <stdio.h>
    
    #define TRUE 1
    #define FALSE 0
    
    typedef int Bool;
    
    main() {
       Bool digit_seen[10] = {0};
       int digit;
       long int n;
    
       printf("Enter a number: ");
       scanf("%ld", &n);
    
       while ( n > 0 ) {
           digit = n % 10; /* 30 % 10 = 0 */
           printf("digit is %d\n",digit);
    /* printf("digi_Seen is %ld", digit_seen_[digit]); */
           if ( digit_seen[digit] ) {
    printf("you suck\n");
                break;
           }
           digit_seen[digit] = TRUE;
    printf("digi_Seen NOW is %s", digit_seen_[digit]);
           printf("n is %ld\n",n);
           n /= 10;
           printf("n is %ld after division\n",n);
       }
    
       if ( n > 0 )  {
          printf("Repeated digit\n\n");
       } else {
          printf("No repeated digit\n\n");
       }
    
       return 0;
    }
    // runnign this

    user1@mint-laptop:~/program/c$ ./repeate_digit
    Enter a number: 123
    digit is 3
    you suck
    Repeated digit

    user1@mint-laptop:~/program/c$

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    This program won't build because it uses an identifier (digit_seen_) that hasn't been declared.

    You're also trying to print out a Bool (which is int) with %s, which expects a char*.

    I would recommend creating a program that builds and runs, and then pasting that exact code. For your reference, when I fixed the problems, the input of 123 gives me "No repeated digit" as the output.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    You are right.

    thank you!!

    Enter a number: 123
    digit is 3
    n is 123
    n is 12 after division
    digit is 2
    n is 12
    n is 1 after division
    digit is 1
    n is 1
    n is 0 after division
    No repeated digit

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    just in case any other newbie like me wanted to see the steps

    Code:
    #include <stdio.h>
    
    #define TRUE 1
    #define FALSE 0
    
    typedef int Bool;
    
    main() {
       Bool digit_seen[10] = {0};
       int digit;
       long int n;
    
       printf("Enter a number: ");
       scanf("%ld", &n);
    
       while ( n > 0 ) {
           digit = n % 10; /* 30 % 10 = 0 */
           printf("digit is %d\n",digit);
    printf("digi_Seen is %d\n", digit_seen[digit]); 
           if ( digit_seen[digit] ) {
    printf("you suck\n");
                break;
           }
           digit_seen[digit] = TRUE;
    printf("---------------------------->  %d\n", digit_seen[digit]);
           printf("n is %ld\n",n);
           n /= 10;
           printf("n is %ld after division\n",n);
       }
       
       if ( n > 0 )  {
          printf("Repeated digit\n\n"); 
       } else {
          printf("No repeated digit\n\n");
       }
       
       return 0;
    }

Popular pages Recent additions subscribe to a feed