0

*/Here is the error I get:

:( identifies 4111111111111113 as INVALID

expected "INVALID\n", not "VISA\n"

:( identifies 4222222222223 as INVALID

expected "INVALID\n", not "VISA\n"

/* //Here is my code:

#include <stdio.h>

#include <cs50.h>

int main(void)

{

long card = 0;
int length;
long check = card;
int position;
int multi;
int sum = 0;
int value = 0;
do
{
    card = get_long("Credit card number:\n"); //get cc number
    long digits = card; 
    for (length = 0; digits>0; length++) //calculate length to proceed
    {
    digits /= 10;
    }
}
while (length < 9 || length > 17);
    for (position =1; position <=length; position++) //checksum
    {
        value = check % 10;
        check /= 10;
        if (position % 2 == 0)
        {
        multi = value*2;
        sum += multi % 10;
        sum += multi /10;
        }
        else if (position % 2 != 0)
        {   
        sum += value;
        }   
    }
if (sum % 10 != 0)
    {
        printf("INVALID\n");
    }
else if (card / 10000000000000 == 34 || card / 10000000000000 == 37) //validate the numbers at the beginning and lengths of cc
{
    printf("AMEX\n");
}
else if (card/ 100000000000000 >= 51 && card / 100000000000000 <= 55)
{
    printf("MASTERCARD\n");
}
else if (card / 1000000000000000 == 4 || card / 1000000000000 == 4)
{
    printf("VISA\n");
}
else
{
    printf("INVALID\n");
}   

}

1 Answer 1

0

You might use the debugger, or you might use "eye-check" (as in read the code :). What is the value of check when program enters the position loop?

You must log in to answer this question.

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