我正在尝试用 C++ 复制 Luhn 算法,但我遇到了问题。如您所见,我有两个 for 循环。在 MSVC 中,如果我在第二个 for 循环中放置一个断点,或者甚至在返回时 MSVC 会告诉我断点不会被击中。
是什么导致了这个问题?
int luhn_checksum(std::vector<int> cardnumber[NUMBER_OF_DIGITS - 1]) {
//step 1: duouble every second number
for (int i = 1; i < NUMBER_OF_DIGITS; i + 2) {
new_digits[i] = digits[i] * 2;
if (new_digits[i] > 9) {
//if the product is larger than 9 we will add the two numbers together
//example: 9 * 2 = 18 so we will add 1 + 8 to get 9
tmp1 += new_digits[i] % 10;
new_digits[i] /= 10;
tmp1 = 0;
}
}
//step 2: sum all the values
for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
checksum += new_digits[i];
}
return checksum;
}
最佳答案
在第一个 for 循环中你没有递增 所以替换
for (int i = 1; i < NUMBER_OF_DIGITS; i += 2)
如果你想增加2
关于c++ - 循环导致无法到达的断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29421712/