Salem, didn't mean any harm,i'm just playing around with it, so that i may understand it. here is your code you post. For example the last digit, if a char X was entered, then it mulitplies it with a 40. I would like it to be a 10. It does check for a toupper 'X', or replaces it with a check =10. Try this number:013288366X,
my sum should be 187, 187%11 =0, then valid isbn.
Code:
#include<stdio.h>
#include<ctype.h>
int testISBN(char*);
int main (void)
{
int isValid;
char code[15];
char temp[100];
do
{
printf("Enter an ISBN (book) number:\n");
fgets(code,sizeof code,stdin);
printf("The ISBN entered is:%s\n",code);
isValid = testISBN(code);
if (isValid==0) {
printf("\n\n\tValid ISBN Number\n");
} else {
printf("\n\n\tInvalid ISBN Number\n");
}
printf("\nDo you wish to continue? (Y/N)\n");
fgets( temp, sizeof temp, stdin );
} while(toupper(temp[0]) != 'N');
return 0;
}
/*
* http://www.morovia.com/supportforum/forum_posts_TID_55_PN_1.html
*/
int testISBN(char*code)
{
int i;
int sum = 0;
int check;
/* sum first 9 digits, as per algorithm */
for ( i = 0 ; i<=9 ; i++)
{
int chVal = code[i] - '0'; /* the numeric value of the char */
int pos = 10-i; /* the numeric value of its position */
sum += chVal * pos;
printf("chval is %d\n,pos is +%d\n",chVal,pos);
}
/* 10th digit is the checksum */
if ( toupper(code[9]) == 'X' ) {
check = 10;
} else {
check = code[9] - '0';
}
printf( "Sum=%d, Sum%%11=%d, check=%d\n", sum, sum%11, check);
/* sum % 11 should be the check */
return ( sum % 11 );
}