for 循环中的变量正在更改,而在 c 中不应该更改

发布于 2025-01-12 22:05:38 字数 2874 浏览 5 评论 0原文

这是代码:

#include <stdio.h> // printf
#include <cs50.h> // get_long
#include <string.h> // strlen
#include <stdlib.h> // stdlib

int credit_test(string input);

int main(void)
{
    string userInput;

    // Gets user input, and tests if input is valid
    bool isInvalid = false;
    do
    {
        userInput = get_string("Number: "); // Prompts user for input

        for(int i = 0, evenIndex = strlen(userInput); evenIndex > i; i++)
        {
            if(userInput[i] - 48 >= 0 && userInput[i] - 48 <= 9 && (strlen(userInput) == 15 || strlen(userInput) == 16)) // Tests if input is valod
            {
                isInvalid = false;
            }
            else
            {
                isInvalid = true;
                break;
            }
        }
    }
    while(isInvalid);

    int keyValidity = credit_test(userInput);
}

int credit_test(string input)
{
    int inputLen;
    inputLen = strlen(input);

    // Even number calculation

    int evenArr[16];

    int evenSum = 0;

    int evenIndex = 0;

    printf("Length: %i\n", inputLen);

    for(int i = 0; inputLen > i; i++)
    {
        int n = i * 2;

        evenArr[evenIndex] = input[n] * 2;

        if(evenArr[evenIndex] > 0)
        {
            evenArr[evenIndex] -= 96;
        }

        if(evenArr[evenIndex] > 9) // Code to split doubles
        {
            int doubleNum = evenArr[evenIndex];

            evenArr[evenIndex] = 1;

            evenIndex++;

            evenArr[evenIndex] = doubleNum % 10;
        }

        evenIndex++;

        evenSum += evenArr[i];

        printf("%i\n", evenArr[i]);
        printf("Length: %i\n", inputLen);
    }

    printf("Length: %i\n", inputLen);

    printf("Even Sum: %i\n", evenSum);

    // Odd number calculation

    int oddArr[16];

    int oddSum = 0;

    int oddIndex = 1;


    for(int i = 0; 16 > i; i++)
    {
        oddArr[i] = input[oddIndex];

        if(oddArr[i] > 0)
        {
            oddArr[i] -= 48;
        }

        oddSum += oddArr[i];

        oddIndex += 2;

        printf("%i\n", oddArr[i]);
    }

    printf("Odd Sum: %i\n", oddSum);

    // Validity test

    int finalSum = evenSum + oddSum;

    int cardType = finalSum % 10;

    printf("Final Sum: %i\n", finalSum);



    if(cardType == 0 && (input[0] - 48) == 5)
    {
        printf("MasterCard \n");
    }else if (cardType == 0 && (input[0] - 48) == 4)
    {
        printf("Visa \n");
    }else if(cardType == 0 && (input[0] - 48) == 3)
    {
        printf("Amex \n");
    }else
    {
        printf("Invalid \n");
    }

    return 0;
}

我只是无法理解为什么,但是如果你运行代码,并密切关注“inputLen”变量,它会保持应有的样子,但在第一个 for 循环中,它会得到偶数输入,inputLen保持不变,这是正确的,但是当循环结束时,由于某种原因,变量变为0?那么有人愿意解释一下为什么会发生这种情况吗?如果代码很奇怪而且很糟糕,我很抱歉:)

非常感谢。

Here is the code:

#include <stdio.h> // printf
#include <cs50.h> // get_long
#include <string.h> // strlen
#include <stdlib.h> // stdlib

int credit_test(string input);

int main(void)
{
    string userInput;

    // Gets user input, and tests if input is valid
    bool isInvalid = false;
    do
    {
        userInput = get_string("Number: "); // Prompts user for input

        for(int i = 0, evenIndex = strlen(userInput); evenIndex > i; i++)
        {
            if(userInput[i] - 48 >= 0 && userInput[i] - 48 <= 9 && (strlen(userInput) == 15 || strlen(userInput) == 16)) // Tests if input is valod
            {
                isInvalid = false;
            }
            else
            {
                isInvalid = true;
                break;
            }
        }
    }
    while(isInvalid);

    int keyValidity = credit_test(userInput);
}

int credit_test(string input)
{
    int inputLen;
    inputLen = strlen(input);

    // Even number calculation

    int evenArr[16];

    int evenSum = 0;

    int evenIndex = 0;

    printf("Length: %i\n", inputLen);

    for(int i = 0; inputLen > i; i++)
    {
        int n = i * 2;

        evenArr[evenIndex] = input[n] * 2;

        if(evenArr[evenIndex] > 0)
        {
            evenArr[evenIndex] -= 96;
        }

        if(evenArr[evenIndex] > 9) // Code to split doubles
        {
            int doubleNum = evenArr[evenIndex];

            evenArr[evenIndex] = 1;

            evenIndex++;

            evenArr[evenIndex] = doubleNum % 10;
        }

        evenIndex++;

        evenSum += evenArr[i];

        printf("%i\n", evenArr[i]);
        printf("Length: %i\n", inputLen);
    }

    printf("Length: %i\n", inputLen);

    printf("Even Sum: %i\n", evenSum);

    // Odd number calculation

    int oddArr[16];

    int oddSum = 0;

    int oddIndex = 1;


    for(int i = 0; 16 > i; i++)
    {
        oddArr[i] = input[oddIndex];

        if(oddArr[i] > 0)
        {
            oddArr[i] -= 48;
        }

        oddSum += oddArr[i];

        oddIndex += 2;

        printf("%i\n", oddArr[i]);
    }

    printf("Odd Sum: %i\n", oddSum);

    // Validity test

    int finalSum = evenSum + oddSum;

    int cardType = finalSum % 10;

    printf("Final Sum: %i\n", finalSum);



    if(cardType == 0 && (input[0] - 48) == 5)
    {
        printf("MasterCard \n");
    }else if (cardType == 0 && (input[0] - 48) == 4)
    {
        printf("Visa \n");
    }else if(cardType == 0 && (input[0] - 48) == 3)
    {
        printf("Amex \n");
    }else
    {
        printf("Invalid \n");
    }

    return 0;
}

I just cannot wrap my head around why, but if you run the code, and keep an eye on the "inputLen" variable it stays what it should be, but in the first for loop which gets the even number in the input, the inputLen stays the same, which is correct, but when the loop finishes, for some reason, the variable changes to 0? So would anyone mind to explain as to why its happening? And sorry if the code is all wonky and bad :)

Thanks so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2025-01-19 22:05:38

循环的这一部分

for(int i = 0; inputLen > i; i++)
{
    int n = i * 2;

    evenArr[evenIndex] = input[n] * 2;

    //...

会调用未定义的行为,因为由于使用表达式 i * 2 作为索引,表达式 input[n] 可以访问已使用数组之外的内存。例如,如果 i 等于 inputLen - 1,则 n 将由表达式 2 * ( inputLen - 1 ) 以及您用作的表达式的值进行初始化用于访问数组 input 元素的索引,但数组没有那么多元素。

此外,在此代码片段中,

    if(evenArr[evenIndex] > 9) // Code to split doubles
    {
        int doubleNum = evenArr[evenIndex];

        evenArr[evenIndex] = 1;

        evenIndex++;

        evenArr[evenIndex] = doubleNum % 10;
    }

    evenIndex++;

变量 evenIndex 可以增加两倍,当该变量用作访问数组 evenArr 元素的索引时,这也可能是未定义行为的原因。 。

This part of the loop

for(int i = 0; inputLen > i; i++)
{
    int n = i * 2;

    evenArr[evenIndex] = input[n] * 2;

    //...

invokes undefined behavior because the expression input[n] can access memory beyond the used array due to using the expression i * 2 as an index. For example then i is equal to inputLen - 1 then n will bi initialized by the expression 2 * ( inputLen - 1 ) and the value of the expression you are using as an index to access elements of the array input but the array does not have so many elements.

Also in this code snippet

    if(evenArr[evenIndex] > 9) // Code to split doubles
    {
        int doubleNum = evenArr[evenIndex];

        evenArr[evenIndex] = 1;

        evenIndex++;

        evenArr[evenIndex] = doubleNum % 10;
    }

    evenIndex++;

the variable evenIndex can be incremented twice that again can be a reason of undefined behavior when this variable is used as an index to access elements of the array evenArr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文