Thread: ISBN Code Checking...!

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You've corrected a few things in the program, but the logic and some other things, are still not OK.

    Try the example input from your first post (or any example input you know the answer to), and SHOW the value you get when it's done. NOT just the "no number" answer, but the ACTUAL value of the ISBN numbers that the program is trying.

    Add some printf()'s followed by getchar()'s, to help you see what your program is actually doing.

    Or run the program in your debugger, step by step, and watch the values, to see where they are going off the rails.

    First goal imo, is to get an example of a good ISBN number, to be marked as valid, by the program. Save the ? logic, until you have the easy stuff working OK.
    Last edited by Adak; 08-25-2012 at 12:28 AM.

  2. #17
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You should run your program through a memory detector such as Valgrind and input several different ISBNs, because this loop:
    Code:
    for(i = 0; i < th2; i++)
                sum = sum + isbn[i]*(i+1);
            for(i = (th2 + 1); i <th1; i++)
                sum = sum + isbn[i]*(i+1);
            for(i = (th1+1); i <10; i++)
                sum = sum + isbn[i]*(i+1);
            sum = sum + 10*(th1+1);
    never resets "i" and can easily go out of bounds of your array. Also, there's a chance that some of the loops won't run, especially the last one as going over 10 seems to be easy.


    Also, there's a few areas that could be changed, but aren't as important:
    Code:
     for(i = 0; i < 10; i++)
        {
              if(isbn[i] == '?')
            break;
        }
        return i;
    could be simplified to:
    Code:
    for (i = 0; i < 10 && isbn[i] != '?'; i++)
        ;
    return i;
    and the same could be applied in a variety of places throughout your program.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Since I can't understand why the position of the X or ? in the ISBN values, makes a difference, the whole if(th1 > th2) block of code, is a mystery to me.

    Here's some pseudo code that I'd suggest studying:

    Code:
    define X 10
    
    This is all in main():
       integers isbn[10]={2,4,6,3,7,8,1,X,9,'?'}; //2463781X95
       integers i, valid=0, sum=0, mysteryNum=0, trial;
    
       for i=0;i<10;i++
          if(isbn[i]==10)
             print "%c ",'X';
          else if isbn[i]=='?'
             print"%c ",'?';
          else
             print "%d ",isbn[i];
       
    
       if isbn[0]==X 
          valid=0;
          print "\n Invalid Code: The ISBN first value, can't be an X.\n";
       
    
       this is the else to the if directly above, and it is the heart of the   
       program:
    
          for i=0,sum=0;i<10;i++   //multiply each digit by it's index +1, but
             if isbn[i] != '?'         //exclude the mystery number, if present
                sum += isbn[i] * (i+1);
             
             else 
                mysteryNum=i+1; //we have a mystery number at i, but it could be at index zero, (which we don't want, so +1
             
          
          print "\nOriginal ISBN sum: %d   Original sum mod 11: %d\n\n",sum, sum%11;
    
          if sum % 11==0  //we have a valid ISBN number
             if mysteryNum //with a mystery number
                mysteryNum=0; //so the mystery number if zero, gives us a valid ISBN number
    
             valid=1;  //and the ISBN number is valid
                 
          else we need to test the mystery number  // ISBN number is invalid - test the mystery array[mysteryNum-1], for values. 
             for i=1;i<11;i++ { //i=0 has already been tested, and failed
                trial = sum + (i*mysteryNum);
                
                /* for debug only (I made an error down below, and this is
                a good way to show how you can find and fix errors)
                if i==2 
                   printf("trial: %d  sum: %d  mysteryNum: %d\n",trial,sum, mysteryNum); getchar();
                 
                */
                if trial % 11==0 
                   valid=1;
                   isbn[mysteryNum-1]=i;
                   sum = trial;
                   break;
                
             end of for
          end of else 
          if sum 
             print "\nFinal ISBN sum: %d   Final sum mod 11: %d\n\n",sum, sum % 11
             if valid
                print "The ISBN code is valid\n"
             else
                print "The ISBN code is invalid\n"
          
             for i=0;i<10;i++ 
                if isbn[i]==10
                   isbn[i]='X';
                if isbn[i]=='?' OR isbn[i]=='X'
                   print "%c ",isbn[i];
                else
                   print "%d ",isbn[i];
             end of for
          end of if
       end of else
       
       printf("\n");
       return 0;
    end of program

  4. #19
    Registered User
    Join Date
    Aug 2012
    Posts
    5
    Quote Originally Posted by Adak View Post
    Since I can't understand why the position of the X or ? in the ISBN values, makes a difference, the whole if(th1 > th2) block of code, is a mystery to me.

    Here's some pseudo code that I'd suggest studying:

    Code:
    define X 10
    
    This is all in main():
       integers isbn[10]={2,4,6,3,7,8,1,X,9,'?'}; //2463781X95
       integers i, valid=0, sum=0, mysteryNum=0, trial;
    
       for i=0;i<10;i++
          if(isbn[i]==10)
             print "%c ",'X';
          else if isbn[i]=='?'
             print"%c ",'?';
          else
             print "%d ",isbn[i];
       
    
       if isbn[0]==X 
          valid=0;
          print "\n Invalid Code: The ISBN first value, can't be an X.\n";
       
    
       this is the else to the if directly above, and it is the heart of the   
       program:
    
          for i=0,sum=0;i<10;i++   //multiply each digit by it's index +1, but
             if isbn[i] != '?'         //exclude the mystery number, if present
                sum += isbn[i] * (i+1);
             
             else 
                mysteryNum=i+1; //we have a mystery number at i, but it could be at index zero, (which we don't want, so +1
             
          
          print "\nOriginal ISBN sum: %d   Original sum mod 11: %d\n\n",sum, sum%11;
    
          if sum % 11==0  //we have a valid ISBN number
             if mysteryNum //with a mystery number
                mysteryNum=0; //so the mystery number if zero, gives us a valid ISBN number
    
             valid=1;  //and the ISBN number is valid
                 
          else we need to test the mystery number  // ISBN number is invalid - test the mystery array[mysteryNum-1], for values. 
             for i=1;i<11;i++ { //i=0 has already been tested, and failed
                trial = sum + (i*mysteryNum);
                
                /* for debug only (I made an error down below, and this is
                a good way to show how you can find and fix errors)
                if i==2 
                   printf("trial: %d  sum: %d  mysteryNum: %d\n",trial,sum, mysteryNum); getchar();
                 
                */
                if trial % 11==0 
                   valid=1;
                   isbn[mysteryNum-1]=i;
                   sum = trial;
                   break;
                
             end of for
          end of else 
          if sum 
             print "\nFinal ISBN sum: %d   Final sum mod 11: %d\n\n",sum, sum % 11
             if valid
                print "The ISBN code is valid\n"
             else
                print "The ISBN code is invalid\n"
          
             for i=0;i<10;i++ 
                if isbn[i]==10
                   isbn[i]='X';
                if isbn[i]=='?' OR isbn[i]=='X'
                   print "%c ",isbn[i];
                else
                   print "%d ",isbn[i];
             end of for
          end of if
       end of else
       
       printf("\n");
       return 0;
    end of program
    I think the position of X and ? do matters... the X could be anywhere except at the front... and also ? could be any where even at the front... So while Multiplying we have to skip the ? and also have to remember it's position so that the MystNumb we have found can be multiplied with the position and added to the sum of rest 9 numbers. and the new sum will be divisible by 11.

    so what about this ex. I copied it from a books ISBN Code....
    8121?2605X

    now the number that I have hided from you is 9. So in place of ? it would be 9. We need to find the numb via C.

    And very much thank you for your response.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In the pseudo code I posted, the position of X and ? are independent of each other.

    It handles the 8121?2605X, just fine, finding the 9.

    Although it's "pseudo code", it's as much code as pseudo code, probably.

    And yes, it's all in C, when you add in the bits and pieces, it will compile!

    You're welcome.

  6. #21
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Tipu Sultan View Post
    the X could be anywhere except at the front...
    As Tim and I have already told you that's wrong. The X is only valid as the last digit (check digit).

    From http://www.isbn.org/standards/home/i...ml/usmapp.htm:
    ...
    3. Construction of an international standard book number
    An international standard book number consists of ten digits[4] made up of the following parts
    ...
    Footnote 4: The digits are the arabic numerals 0-9; in the case of the check digit only, an X can sometimes occur (see 2.4)
    Bye, Andreas

  7. #22
    Registered User
    Join Date
    Aug 2012
    Posts
    5
    Quote Originally Posted by AndiPersti View Post
    As Tim and I have already told you that's wrong. The X is only valid as the last digit (check digit).

    From http://www.isbn.org/standards/home/i...ml/usmapp.htm:


    Bye, Andreas
    THANKS A LOT... that reduces my work a lot more... now we just need to deal with 9 numbers and accordingly no need to deal with the position of the X... Thanks again....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking for a code in a string
    By shin_ono in forum C Programming
    Replies: 13
    Last Post: 10-01-2008, 02:04 AM
  2. Checking lines of code
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-10-2008, 02:05 AM
  3. Looking for Code Analysis / Checking Tool
    By nvoigt in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-01-2007, 03:34 AM
  4. Please restore the code tag checking script
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 76
    Last Post: 09-09-2006, 12:35 AM
  5. file existance checking /w code
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 02:40 PM

Tags for this Thread