Need a program that validates both ISBN-10 and ISBN-13 from user input. Must follow the instruction below, its an assignment for a class and I have a family emergency I have to go out of town for and don’t know how else to get it done. The instructions are as follows:
INSTRUCTIONS
Write a program which asks for an ISBN number, then strips the
last digit (saving it) then re-calculates that last (check) digit from
the rest of the number. If they match, the function will validate the
ISBN number, otherwise the function will sound an alarm. See the bottom of this file for some sample runs.
Specific directions:
Create a function:
bool chk_isbn(string)
which receives a String Object containing user keyboard input. If the
user is in hir right mind and the keyboard is cooperating, the string
SHOULD contain a valid ISBN (International Standard Book Number).
The chk_isbn() function should parse the number, and calculate the
check digit from the first 10 data characters, ignoring dashes, spaces,and all non-digits in the first 9 columns. Once it has calculated the check digit, it should compare its answer with the check digit in the last column of the String Object. If they agree, the function should return true, otherwise it should return false.
Include a main() function which exercises the chk_isbn()
function, accepting command-line or interactive input from
the user. CLEAN UP THE USER INPUT so that what you pass to the
chk_isbn() function is ONLY a string of digits, no hyphens
or any other non-digit characters.
Your program will be tested against valid ISBNs from the real world,
and invalid ones. It should give the proper output in all cases.
Samples of VALID input:
“0-321-40939-6″
“ISBN 0-321-40939-6″
“ISBN 0 321 40939 6″
“0.321.40939.6″
“0321409396″
See the situation here? We are accepting input from USERS, and about
all we can demand from a USER is that s/he gets the digits right.
They will use any random character for the separator, or none at all,
so we have to write our code to cope. If a user can break our software, or get it to emit the wrong answer, we lose.
So your first task will be to “condition” the input. Lucky for
you, you’re taking this into a string object, and strings have the
string.erase(start,length) method to make it easy to “remove” characters from the string which don’t belong there using something like:
for ( int x = 0 ; x < 10 ; x++)
if (!isdigit(stringvar[x])
stringvar.erase(x, 1) ;
We ALWAYS ALWAYS ALWAYS have our USERS enter their input into
CHARACTER STRINGS. Then, if we are expecting a number from
them, we can examine and “validate” user input before risking
our program’s health by computing with unknown input.
USERS take a little management, and the programmer who learns
this Grate Trooth early will avoid much heayd hoortingz.
Here is some background information on ISBNs, which
illustrates how often even “learned” posters don’t
validate their factoids:
“ISBN is the abbreviation for the International Standard Book
Number. ISBN numbers are 10 digits in length. In an ISBN of
the form X-XX-XXXXXX-X:
-The first block of digits on the left represents the language
of the book (0 is used to represent English). This block is
usually 1 digit in length.
-The second block of digits represents the publisher. This
block is usually 2 or 3 digits in length
-The third block of digits represents is the number assigned
to the book by the publishing company. This is usually 5 or
6 digits in length.
-The fourth block consists of the check digit.”
(from: http://www.cs.queensu.ca/~bradbury/checkdigit/isbncheck.htm)
“An International Standard Book Number consists of 10 characters of information, organized into 4 groups with adjacent groups separated by a dash character.
The first group identifies the continent of publication,
the second group identifies the publisher,
the third group is the book number that the publisher assigns and
the last group with one character is a check digit determined by an algorithm.
Each non dash character, except possibly the last is a digit.
The last is either a digit or the character X for ten.
The value of the check digit comes from the following algorithm.
The ISBN is a number composed of digits 0-9 and X in
either 10 or 13 columns. The LAST COLUMN contains a
“check digit” which is computed from the other 9 or 12
digits. It is computed in a way that will reveal if
numbers are transposed (swapped) by careless humans.
Initialize sum to zero.
for i from 1 to 9(length of number string)
Calculate C = the number in COLUMN number i * COLUMN
Increase the sum by the product of the COLUMN and the value
of the digit in column i of the book number.
Reduce the sum mod 11.
(Replace the sum by its remainder when you divide the sum by 11.)
The value of the check digit C equals 11 – sum mod 11.
If C is 10, then C = the character X>
Example:
I will write down an arbitrary 9 digit number and compute its check digit.
digits = 918273645
Simulate the algorithm.
i digit[ i ] sum
0
1 9 0 + 9 * ( 11 – 1 ) = 90 mod 11 = 2
2 1 2 + 1 * ( 11 – 2 ) = 2 + 9 mod 11 = 11 mod 11 = 0
3 8 0 + 8 * ( 11 – 3 ) = 0 + 64 mod 11 = 64 mod 11 = 9
4 2 9 + 2 * ( 11 – 4 ) = 9 + 14 mod 11 = 23 mod 11 = 1
5 7 1 + 7 * ( 11 – 5 ) = 1 + 42 mod 11 = 43 mod 11 = 10
6 3 10 + 3 * ( 11 – 6 ) = 10 + 15 mod 11 = 25 mod 11 = 3
7 6 3 + 6 * ( 11 – 7 ) = 3 + 24 mod 11 = 27 mod 11 = 5
8 4 5 + 4 * ( 11 – 8 ) = 5 + 12 mod 11 = 17 mod 11 = 6
9 5 6 + 5 * ( 11 – 9 ) = 6 = 10 mod 11 = 16 mod 11 = 5
C = 11 – 5 mod 11 = 6 “
from: http://www.cbu.edu/~yanushka/j0/n.3
Extra Credit: on your own, research and impliment ISBN-13 logic,
and write your program so that it can process either ISBN-10 or
ISBN-13 without any special input from the user other than the number
itself.
Sample data I will use to test your programs:
0-13-615250-3
0-13-615250-8
0-13-613950-X
0-13-615250-X
–0—13-6–15–250-3
0–13—6 152 50.3
…and similar.
Here Are Some Sample Runs:
Script started on Mon 13 Apr 2009 07:25:32 PM PDT
ISBN: 0-13-615-25-8
0-13-615-250-8
0-13-615-250-8
013-615-250-8
013-615-250-8
013-615-250-8
013615-250-8
013615-250-8
013615-250-8
013615-250-8
013615250-8
013615250-8
013615250-8
013615250-8
0136152508
Stripped checkdigit: 8
checking: 013615250
sum: 124
MODDED sum: 3
Original checkdigit: 8 newly calculated checkdigit: 3
ERROR: BAD ISBN CHECKDIGIT!!
: Success
Updated/recalculated ISBN: 0136152503
ISBN: 0-13-615-250-3
0-13-615-250-3
0-13-615-250-3
013-615-250-3
013-615-250-3
013-615-250-3
013615-250-3
013615-250-3
013615-250-3
013615-250-3
013615250-3
013615250-3
013615250-3
013615250-3
0136152503
Stripped checkdigit: 3
checking: 013615250
sum: 124
MODDED sum: 3
Original checkdigit: 3 newly calculated checkdigit: 3
Updated/recalculated ISBN: 0136152503
ISBN: –0–13–615-250-3
–0–13–615-250-3
-0–13–615-250-3
0–13–615-250-3
0–13–615-250-3
0-13–615-250-3
013–615-250-3
013–615-250-3
013–615-250-3
013-615-250-3
013615-250-3
013615-250-3
013615-250-3
013615-250-3
013615250-3
013615250-3
013615250-3
013615250-3
0136152503
Stripped checkdigit: 3
checking: 013615250
sum: 124
MODDED sum: 3
Original checkdigit: 3 newly calculated checkdigit: 3
Updated/recalculated ISBN: 0136152503
ISBN: 9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
9780814408902
Stripped checkdigit: 2
checking: 978081440890
sum: 332
MODDED sum: 2
Original checkdigit: 2 newly calculated checkdigit: 2
Updated/recalculated ISBN: 9780814408902
ISBN: 9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408–90-2
9780814408-90-2
978081440890-2
978081440890-2
978081440890-2
9780814408902
Stripped checkdigit: 2
checking: 978081440890
sum: 332
MODDED sum: 2
Original checkdigit: 2 newly calculated checkdigit: 2
Updated/recalculated ISBN: 9780814408902