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.
Any help would be appreciated.
title; /*book title*/
printf("\nPlease enter the title: ");
printf("\nPlease enter the isbn: ");
for(isbn_ptr = isbn; *isbn_ptr != '\0'; ++isbn_ptr)
/*goes to end of string and finds check digit*/
check_digit = *isbn_ptr;