0

I was trying to solve this problem. I did some coding:

#include <cs50.h>
#include <stdio.h>

int get_digit_number(long card_number);

int main(void){

bool valid;
long card = get_long("Enter card number: ");
long card2 = card / 10;
long card3 = card;
int digit_number = get_digit_number(card);
if (digit_number != 13 || digit_number != 15 || digit_number != 16)
{
    valid = false;
    printf("INVALID");
}

int digits1 = 0;
int digits2 = 0;
int sum1 = 0;
int sum2 = 0;
int card_brand;

for(int i = 0; i <= digit_number; i + 2)
{
    //first sum
    digits1 = card3 % 10;
    card3 /= 100;
    sum1 += digits1;

    //second sum
    digits2 = card2 % 10;
    digits2 *= 2;
    if (digits2 >= 10)
    {
        sum2 = digits2 % 10;
        digits2 -= digits2 % 10;
        digits2 /= 10;
        sum2 += digits2;
    }
    else{
        sum2 += digits2;
    }
    card2 /= 100;
}

for (int k = 0; k < digit_number - 2; k++)
{
    card_brand = card / 10;
}

if ((sum1 + sum2) % 10 != 0)
{
    valid = false;
    printf("INVALID\n");
}
else {
    valid = true;
    printf("VALID\n");
}

if (valid && (((card_brand - (card_brand % 10)) / 10  == 4)))
{
    printf("VISA\n");
}
else if (valid && (card_brand > 50 && card_brand < 56))
{
    printf("MASTERCARD\n");
}
else if (valid && (card_brand == 34 || card_brand == 37))
{
    printf("AMEX\n");
}
}

int get_digit_number(long card_number)
{
    int y = 1;
    while (card_number >= 10)
    {
        card_number /= 10;
        y++;
    }
    return y;
}

but when I try to make credit2.c, it gives me two errors:

credit2.c:20:16: error: variables 'i' and 'digit_number' used in loop condition not modified in loop body [-Werror,-Wfor-loop-analysis] for(int i = 0; i <= digit_number; i + 2)

credit2.c:20:37: error: expression result unused [-Werror,-Wunused-value] for(int i = 0; i <= digit_number; i + 2)

What is my problem here? I'm a total noob btw. Thanks in advance.

1 Answer 1

1

It's a problem with the for statement setup. Look at the statement:

for(int i = 0; i <= digit_number; i + 2)

The increment clause i + 2 isn't properly written. This takes the value in i and adds it to 2, but it doesn't say where to store the result or what to do with it. This also causes the expression result unused error. Things like i++ work because the ++ operator automatically stores the result back in i. In this case, the code should store the result back in i, like this: i = i + 2.

The compiler is detecting this. It sees that i is not being incremented anywhere, so i never changes. So, for the loop to terminate, the test condition has to have a way to be satisfied. That means that digit_number has to change, but the compiler sees that it doesn't change inside the loop either. Since neither i nor digit_number can change, this would cause an infinite loop and the compiler throws an error because of it.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you! I noticed and corrected some of my other mistakes in the code and it works fine now! Sep 1, 2022 at 19:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .