Processing a book ISBN

This is a discussion on Processing a book ISBN within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to figure out this program. It accepts an ISBN (International Standard Book Number) from the user, ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    Processing a book ISBN

    Hi,
    I am trying to figure out this program. It accepts an ISBN (International Standard Book Number) from the user, and then decides whether it was entered correctly or not based on the formula used in calculating an ISBN. An ISBN is a 10 digit number that uses the last digit as error correction. So if the ISBN is 0132261197, its calculated as:
    1*0 + 2*1 + 3*3 + 4*2 + 5*2 + 6*6 + 7*1 + 8*1 + 9*9 = 161. The last digit 7, which is the check digit, is the remainder when 161 is divided by 11. So if the numbers are entered incorrectly, the remainder would not match the check digit, causing the program to report an incorrect ISBN.

    I have the program working up until the check digit is found in the string and assigned to a variable for later comparison. What I can't get right is the way that the math is done here. The book says to store the ISBN in a character array, then do the summation using the fact that the numeric value of a digit character can be had by subtracting the character '0' from the character. Like '4' - '0' is 4. I don't understand how to set this up for the string. Anyway, here is what I have so far.
    I know I shouldn't use gets, but I haven't learned how to use fgets yet.
    Code:
    #include <stdio.h>
    
    main()
    
    {
        char  isbn[52],
               *isbn_ptr,
                check_digit,              
                 title[52];            /*book title*/
        
                            
             printf("\nPlease enter the title: ");
             gets(title);
             
             printf("\nPlease enter the isbn: ");
             gets(isbn);
             
                for(isbn_ptr = isbn; *isbn_ptr != '\0'; ++isbn_ptr)
                ;
                                         /*goes to end of string and finds check digit*/
            --isbn_ptr;
            check_digit = *isbn_ptr;
    
    }
    Any help would be appreciated.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I'd suggest reading this and this, as well as your text, until it makes sense. You could submit something like this, but who knows if your instructor might ask if you can explain it.
    Code:
    int valid_isbn(const char *text)
    {
       int i, sum = 0;
       for (i = 0; i < 9; ++i)
       {
          sum += (text[i] - '0') * (i + 1);
       }
       return sum % 11 == text[i] - '0';
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125

    Search this board....

    Also, try searching this board.

    I seem to recall a post about ISBN parsing posted to the board within the last year.....
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    I pretty much understand that whole thing, but how does that return statement work: The remainder of sum / 11 is equal to text[] - '0' ? If we used 0132261197 for the ISBN, Text[] would equal 9 at the end of the loop. So how would that equal the remainder of sum / 11?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    161 / 11 = 14
    14 * 11 = 154
    161 - 154 = 7

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    That's all true, but how is text[] ever going to equal the remainder? Is there a condition that has to be met? I don't know how the return statement is read.

  7. #7
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    Code:
    return ((sum % 11) == (text[i] - '0'));
    This means 0 is returned if an error is found.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Why is text[i] == 9? It == 7. The loop will increment i to the last digit (ie the check digit), check the for() conditional, find that i is 9, and exit. Still, you can clear things up by writing instead

    Code:
    return sum % 11 == text[9] - '0';


    And of course, loop unrolling

    Code:
    int valid_isbn(const char *isbn)
    {
        return (isbn[0] + isbn[1]*2 + isbn[2]*3 + isbn[3]*4 + isbn[4]*5
            + isbn[5]*6 + isbn[6]*7 + isbn[7]*8 + isbn[8]*9 - '0') % 11
    		== ((isbn[9]=='X'||isbn[9]=='x')?10:(isbn[9]-'0'));
    }
    Where X/x is 10 in base 11, of course. You missed it Dave
    Last edited by jafet; 08-11-2006 at 03:33 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Too bad your unrolled loop is wrong. You're multiplying the value of each character. See, when you're trying to be clever, it actually helps if you are.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Too bad your unrolled loop is wrong.
    Prove it.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is 1 * 2 + 3 * 3?
    What is 49 * 2 + 51 * 3 - 48?
    Are the two equal?
    If I add a number to a total ten times, then subtract it once, is that the same thing as adding it ten times and subtracting it ten times?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jafet
    Still, you can clear things up by writing instead
    Code:
    return sum % 11 == text[9] - '0';
    I wasn't trying to be clear.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Quote Originally Posted by richdb
    If we used 0132261197 for the ISBN, Text[] would equal 9 at the end of the loop. So how would that equal the remainder of sum / 11?
    I thought then you were just trying to make me think.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Alright, I was trying to be clever. But the code is correct, if not intuitive. Remember that we are subtracting '0' a total of 45 times from the LHS, and 44n % 11 == 0. So we only need to subtract '0' once.
    Last edited by jafet; 08-12-2006 at 12:17 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 06:46 AM
  2. Can't display book and borrower's details.
    By grscot in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2003, 11:18 AM
  3. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM

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