IncognitoSpeedy
IncognitoSpeedy

Reputation: 37

A variable in a for loop is changing without being supposed to change in c

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.

Upvotes: 1

Views: 655

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

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.

Upvotes: 1

Related Questions