# Back to ISBN

I was working for a long time with this.

The isbn program, which I am working on my own....this is how far i gotten.

its more or less a progress check to prove i am working on it, i know nietod and others may thought i was handing in mflams in...but his is way beyond my course of this semester....

i am stuck as the checksum is incorrect...and there is a slight problem, if the reaminder is 10, the checksum is X not 10.  so i need help with that and getting this to work now.

thanks

#include <iostream.h>
#include <iomanip.h>

int main()
{
char isbn[10];
char input;
int i;
cout << "Input ISBN number: ";

for (i = 0; i < 10; i++)
{
cin >> input;
int(char(input));
isbn[i] = input;
}
cout << endl;
int sum, digit, correct;
for (i = 0; i < 9; i++)
sum += (i+1) * digit;
correct = sum % 11;

if (isbn[9] == correct)
cout << "Correct!\n";
else
cout << "correct checksum is: " << correct;

cout << endl << endl;

return 0;
}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Are you sure this is the algorhitm?  What should X be if the remainder is 10 (X is what you refer to in your text, not your code)?
0
Author Commented:
Yeah, I believe so...I never coded the X yet. I want this to work before I deal witht the X

1 · 0 + 2 · 6 + 3 · 7 + 4 · 0 + 5 · 8 + 6 · 2 + 7 · 1 + 8 · 6 + 9 · 2 = 158

158 % 11 = 4

if n % 11 = 10, then the isbn checksum is X, not 10.

0
Commented:
Can you get your program to work when the checcksum is not X?
do you know how to get digit from isbn[]?
0
Author Commented:
it does not work at all....for x or not
0
Commented:
in your sum loop, you are not setting digit
(you should also be initializing sum to 0 before starting the loop)
0
Commented:
Checking it by hand works for the three books I've tried.  There are, however, a couple of places in your code which need work.

Initialize all variables.  Don't trust the compiler.

Check to be sure what you think is going to be a digit really is.

cout (or printf) is cheap.  It will save you hours++ of agony, and bloodstains where you were beating your head against the wall.

More after I code your sample my way.
0
Commented:
Ah, how quickly the old hands overlook the obvious!  Guilty am I!

What you really mean to store in isbn[i] is input - '0'.  The compiler lets us get away with
int(char(input) );
it is a subtle bug.  Were you thinking that that line was converting the value of input from the ASCII code for the character to the integer?  (That is, the character '5' would become the integer 5?)

As mentioned earlier, be sure you really have a digit (you can use isdigit() to check).

BTW, you should stick to endl when using cout.  Mixing endl and '\n' may get you strange results.

Thanks for posting the ISBN checksum!  Would you please tell us what X should be when the remainder is 10?  Thanks!
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
To be explicit:
The first for() loop should contain
isbn[i] = input - '0';
int(char(input));
isbn[i] = input;
and the second loop should contain
sum += (i+1) * isbn[i];
and get rid of the variable "digit".

You must initialize the variable "sum".

Here's some more thoughts:
If you always put curly braces in an if (or a loop, etc.) then you'll never have to worry about adding them when adding debug couts or printf()s.  Also, it will make the test condition stand out in the code, with or without indentation.

When comparing a constant with a variable (which you aren't in this code!), put the constant on the left of the == or != so that the compiler will remind you if you accidentally use a single = (assignment) instead of == (test for equality) or != (test for inequality).

The habit of beginning each variable name with a letter which indicates its type is A Good Thing.  cInput would help remind us that it holds a character.  iSum would hold an integer, szMyString would be a zero-terminated string, and bIsCorrect would be a boolean.  Keep in mind, though, case sensativity, and use it to your advantage.
0
Commented:
1. Initialize your isbn array: eg
for i = 1 to 10
isbn[i] = 0 // that's pseudo code...

2. Initialize your other variables: eg
char bla=' ';
int blu = 0;
IMPORTANT: This includes the sum and other variables you added in the middle of your code.
Either add lines like: sum = 0;
Or declare you variables:
int sum = 0;
(When I tried your code, the sum started off at -896755460 or something like that. Then it did: sum += 1*1...

3. You wrote:
sum += (i+1) * digit;
You forgot to give digit it's value.
You meant probably
...          * isbn[i];

You could find this mistake when you compile and
a. use a debugger to look at digit.
b. print out all the values (if you don't know how to use a debugger:
eg.
cout << " sum:" << sum;
cout << " i: " << i;
cout << " digit: " << digit;
cout << endl;

c. your compiler probably warned you that "local variable 'digit' used without having been initialized"
or something like that.

4. What I just said about
...      * isbn[i];
will not work just yet.
isbn[i] has a char with the value of
'1' or '2'...
You must convert it into the real digit.
char '0'  is int 48
char '1'  is int 49
etc.

So what you have to do is convert by adding the following:
for ...
{
digit = isbn[i] - 49;
... * digit;
}

All this said and done, you have not checked if people gave you less than 10 digits, or more, or put all kinds of letters inside, but it seems robust if you followed the preceding fixes.

Add an if clause that if the correct is 10 check if the isbn[9] equals 'x' or 'X'.

Happy to see you sweating,  Moshe
0
Author Commented:
X is remainder 10
0
Commented:
Moshe is correct about nearly everything.  You do not need to initialize isbn[] elements; you'll be calculating values for them and not referencing them before you populate the array.  (Also, you should not waste time in a loop initializing an array; there are better ways.)

I was wrong when I played fast and loose by storing an integer (for example, 1 instead of '1') in the isbn[] array.  Sorry!  You can do the conversion at the time of calculation (keeping isbn[] an array of char) or do the conversion early by using an array of int.  If I've confused anybody, I'm sorry!

When converting characters, it is best to use character values ('0' [which the compiler will convert to an integer]) rather than magic numbers (48 decimal [or hex 30 {0x30 would be the proper notation to use}] is the ASCII code for the digit zero).  (Moshe's code would subtract the ASCII value of the character '1' (decimal 49, [hex 31]) from the entered digit!  A great example of why you should NOT use numbers!)
0
Author Commented:
this is what i got so far, i just forgot to make sum = 0...ooops!!!

i am jsut stuck with the X part.

thnx all 4 ur patience with me, as i am trying to learn this languge!

#include <iostream.h>
#include <iomanip.h>

int main()
{
char isbn[10];
char input;
int i;
cout << "Input ISBN number: ";

for (i = 0; i < 10; i++)
{
cin >> input;
isbn[i] = input - '0';
}
cout << endl;
int sum = 0, correct;

for (i = 0; i < 9; i++)
sum += (i+1) * isbn[i];

correct = sum % 11;

if (isbn[9] == correct)
cout << "Correct!\n";
else
cout << "Correct checksum is: " << correct;

cout << endl << endl;

return 0;
}
0
Author Commented:
thank u....it got me this far.

now i can incorporate moshes comment to finish it off....thnx all

i will post the code for finished programm if possible.

tnx
0
Commented:
Thank you for the points!  If you have any difficulty, PLEASE feel free to ask for help!  I really do enjoy helping people who are willing to try working on a project themselves before they hollar for help.

BTW, remember the variable "correct" contains an integer, not a character.

One of the "advantages" of printf() over cout is that the programmer is forced to remember what kind of data are being output:
printf("%s\n", szMyString);
for a string, versus
printf("%c\n", cMyCharacter);
for a character, and
printf("%d\n", iMyInteger);
for an integer.  cout will let the programmer be blissfully unaware of the variable's type....
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.