c++ - 循环导致无法到达的断点

标签 c++ visual-c++

我正在尝试用 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/

相关文章:

c++ - 有一个很长的缓冲区,但只使用最后 1GB 字节的数据。

c++ - 为什么复制省略对形式参数进行异常(exception)处理?

visual-studio-2010 - 单个进程可以加载/使用 msvcr100.dll 和 msvcr110.dll 而不会引起问题吗?

c++ - 在 MSVC 中解析命令行

c++ - 指针是如何工作的

c++ - 使用 char 确定输出不起作用

c++ - 如何在 std::string 和 Aws::String 之间进行转换?

c++ - 使用 extern c 和 dllexport 与模块定义 (msvc++) 进行标准调用名称修改

c++ - 就地成员初始化和聚合初始化

c++ - 如何将击键发送到其他进程(例如记事本)?