C++计算总是打印出11

Ben*_*zel 0 c++

我目前正在编写一个程序,根据用户输入的ISBN的一部分提供校验和.我对13位ISBN的计算给了我正确的答案,但由于某种原因,无论我输入什么,10位数的计算总是给我11.

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;
Run Code Online (Sandbox Code Playgroud)

有什么简单的东西我错过了吗?我在这里先向您的帮助表示感谢.

krz*_*zaq 13

您似乎误解了运算符的,工作原理.(2, 3)收益率3,而不是像Python那样的元组.

同样的(2,3,4) * (1,2,3)情况几乎相当于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))
Run Code Online (Sandbox Code Playgroud)

没有什么不同

checksum = (11 - ((2 * digit_nine) % 11)) 
Run Code Online (Sandbox Code Playgroud)

然后可以安全地假设你digit_nine0¹,这就是为什么最终checksum有价值的原因11 - 0

¹或11根本不是数字的乘法