CS50 PSET 1 Hacker Bad Credit Code

he goal is to find if a credit card number is valid.

Complete Instructions Here:

https://cdn.cs50.net/2015/x/psets/1/hacker1/hacker1.html#bad_credit

Can I please know what makes my code fail?

(I dont get running errors)

My code: #include #include #include #include #include #include

int main(void) {

char name[20];
char dist[3];
long long number = GetLongLong();

sprintf(name, "%lld", number );

int length = strlen(name);

if ( (length != 13) || (length != 15) || (length != 16) )
{
printf("INVALID");
return 1;
}

int SUM = 0;
if ( (length == 13) || (length == 15) )
{
for (int a = 1; a < length-1; a+=2)
{
int prodig = (name[a]-48)* 2;
sprintf(dist, "%d", prodig);
if (strlen(dist) == 2)
{
SUM = SUM + (dist[0]-48) + (dist[1]-48);
}
else
{
SUM += prodig;
}
}

for (int a = 0; a < length; a+=2)
{
    SUM += (name[a]-48);
}

}

if (length == 16)
{
for (int a = 1; a < 17; a+=2)
{
int prodig = (name[a]-48)* 2;
sprintf(dist, "%d", prodig);
if (strlen(dist) == 2)
{
SUM = SUM + (dist[0]-48) + (dist[1]-48);
}
else
{
SUM += prodig;
}
}

for (int a = 0; a < 15; a+=2)
{
    SUM += (name[a]-48);
}

}

if (SUM%10 != 0)
{
printf("INVALID");
return 1;
}

else
{
if (name[0] == 3)
{
printf("AMEX");
}

else if (name[0] == 5)
{
    printf("MASTERCARD");
}

else
{
    printf("VISA");
}

}

return 0;
}

Output of test (on cs50 site):

$ check50 2015.fall.hacker1.credit credit.c

:) credit.c exists

:) credit.c compiles

:( identifies 378282246310005 as AMEX \ expected output, not a prompt for input

:( identifies 371449635398431 as AMEX \ expected output, not a prompt for input

:( identifies 5555555555554444 as MASTERCARD \ expected output, not a prompt for input

:( identifies 5105105105105100 as MASTERCARD \ expected output, not a prompt for input

:( identifies 4111111111111111 as VISA \ expected output, not a prompt for input

:( identifies 4012888888881881 as VISA \ expected output, not a prompt for input

:( identifies 1234567890 as INVALID \ expected output, not a prompt for input

:( rejects a non-numeric input of "foo" \ expected output, not a prompt for input

:( rejects a non-numeric input of "" \ expected output, not a prompt for input

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories