Benzel - 1 year ago 73
C++ Question

# C++ Calculation always printing out 11

I'm currently writing a program to give the user a checksum based off numbers from a part of an ISBN that they input. My calculation for the 13-digit ISBN is giving me the right answers, but for some reason, the calculation for the 10-digit is always giving me 11, no matter what I put in.

``````cout << "Enter the digits one at a time, as if they were to be read from right to left." << endl;

cin >> digit_one;
cin >> digit_two;
cin >> digit_three;
cin >> digit_four;
cin >> digit_five;
cin >> digit_six;
cin >> digit_seven;
cin >> digit_eight;
cin >> digit_nine;

checksum = (11 - (((10, 9, 8, 7, 6, 5, 4, 3, 2) * (digit_one, digit_two, digit_three, digit_four, digit_five, digit_six, digit_seven, digit_eight, digit_nine)) % 11));

cout << "The checksum for this ISBN is " << checksum << endl;
``````

Is there something simple that I'm missing? Thanks for the help in advance.

You seem to misunderstand how operator `,` works. `(2, 3)` yields `3`, not a tuple like it would in Python.

In the same vein `(2,3,4) * (1,2,3)` is pretty much equivalent to `4 * 3`.

``````checksum = (11 - (((10, 9, 8, 7, 6, 5, 4, 3, 2) * (digit_one, digit_two, digit_three, digit_four, digit_five, digit_six, digit_seven, digit_eight, digit_nine)) % 11))
``````

is no different than

``````checksum = (11 - ((2 * digit_nine) % 11))
``````

It's then safe to assume that your `digit_nine` is `0`¹ and that's why finally `checksum` has the value of `11 - 0`

¹ or a multiplication of `11` and not a digit at all

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download