Home > front end >  CS50:Credit. Why is "4062901840" not getting evaluated as INVALID, if every other 10 digit
CS50:Credit. Why is "4062901840" not getting evaluated as INVALID, if every other 10 digit

Time:06-11

After resolving the CS50 credit problem set, my only problem is that card number 4062901840, does not pass the test. Running check50 says that after enter this number, it should print "Invalid" (which is correct, is a 10 digit number) but instead prints " ".

Already ran 20 tests using 10 digit numbers and all 20 passed the test correctly, the code is pretty simple since i tried to use only what we´ve been taught so far and don´t use arrays or other libraries. Can you point me in the right direction to know why this is happening?

The program should ask for a number, and evaluates if it meets the luhn algorithm, which is:

  • Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.

  • Add the sum to the sum of the digits that weren’t multiplied by 2.

  • If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

  • After that, the program should check if the card number is amex, visa, mastercard or invalid

The conditions for every card type are:

-American Express (15 digits) Start with 34 or 37

-Mastercard (16 digits) Start with 51, 52, 53, 53 OR 55

Visa(13 or 16 digits) Startt with 4

Here is my code

#include <cs50.h>

#include <stdio.h>

int main(void) {

long cnum, cnumClone;

int count, first, tempo, sum = 0;

do{
    printf("Enter card number\n");
    scanf("%ld", &cnum);
} while(cnum == 0);

// Clones card number to manipulate it through the iteration
cnumClone = cnum;
//Count every digit of the entered card number
for(count = 1; cnumClone != 0; count  )
{
    //Get the last digit
    tempo = cnumClone % 10;
    //Remove last digit
    cnumClone /= 10;
    //Select digits to be multiplied
    if(count % 2 == 0)
    {
        tempo *= 2;
        //In case the product is a 2 digit number
        if (tempo >=10)
        {
            tempo = tempo % 10;
            tempo  = 1;
            sum  = tempo;
        }else{
            //Add to the sum
            sum  = tempo;
        }
    }else{
        //Add to the sum directly if it wasn´t a every other digit
        sum  = tempo;
    }
}
//Last step of Luhn´s algorithm
if (sum % 10 == 0)
{
    //Since count initiates on 1 for iteration purposes, take away 1
    count -= 1;
    // If card number length is 16 checks if it´s a mastercard or visa
    if(count == 16)
    {
        first = cnum / 100000000000000;
        if(first == 51 || first== 52 || first == 53 || first == 54 || first == 55)
        {
            printf("MASTERCARD\n");
        }else{
            first = first /10;
            if(first == 4)
            {
                printf("VISA\n");
            }else{
                printf("INVALID\n");
            }
        }
    }
    // If card number length is 15 checks if it´s an amex
    if(count == 15)
    {
        first = cnum / 10000000000000;
        if(first == 34 || first == 37)
        {
            printf("AMEX\n");
        }else{
            printf("INVALID\n");
        }
    }
    // If card number length is 15 checks if it´s a visa
    if (count == 13)
    {
        first = cnum / 1000000000000;
        if(first == 4)
        {
            printf("VISA\n");
        }
    }
}else{
    //If card number has a length different than 13, 15 or 16
    printf("INVALID\n");
}

}

Thanks in advance, hope i use this forum the right way.

CodePudding user response:

The problem is here

}else{
    //If card number has a length different than 13, 15 or 16
    printf("INVALID\n");
}

What does the first } in the phrase close? Hint: it has nothing to do with the length. Suggest the length tests be if...else if...else if...else; the (new) final else outputs the INVALID message.

  • Related