hey ,guys,i'm stuck trying to validate a ISBN number(w/o hyphens). I want it to check the last digit for a X and update the sum. But i keep getting the wrong sum. What's wrong in my code?
[code]
#include<stdio.h>
#include<ctype.h>
Code:
int testISBN(char*);
int main (void)
{
int divisible;
char choice, trash;
char code[15];
do
{
printf("Enter an ISBN (book) number:\n");
fgets(code,15,stdin);
printf("The ISBN entered is:%s\n",code);
divisible = testISBN(code);
if (divisible == 1)
printf("\n\n\tValid ISBN Number\n");
else
printf("\n\n\tInvalid ISBN Number\n");
printf("\nDo you wish to continue? (Y/N)\n");
choice = getchar();
trash = getchar();
}while(toupper(choice) != 'N');
return 0;
}
int testISBN(char*code)
{
int i;
int sum = 0;
int value;
for(i=9;i>=0; i--)
{
if((i==9) && (toupper(*(code + i)) == 'x'))
{
value = 10;
}
else
{
value = (int)(*(code + i)-48);
}
sum += value * (i+1);
}
printf("sum is %d",sum);
if ((sum%11) == 0)
return 1;
return 0;
}