I just started this course and have been enjoying it very much. I complete pset1 credit with the following code. But when I check my solutions, it always returns: Has anyone encountered this?
:) 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
Code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
long long cardnumber = 0;
long long digits = 0;
//check long long is greater than 0 and digit length//
cardnumber = get_long_long();
//calculate long long digit lenth//
long long cardnumberl = cardnumber;
while (cardnumberl > 0)
{
digits++;
cardnumberl = cardnumberl / 10;
}
//check digit length//
if (digits != 13 && digits != 15 && digits != 16)
{
printf("INVALID\n");
return 0;
}
//calculate sum of first and second set of digits//
long long checksum1 = 0;
long long checksum2 = 0;
long long cardnumber1 = cardnumber;
long long remainder;
long long remainder1;
long long remainder2;
int i;
for (i = 0; i < digits; i++)
{
remainder = cardnumber1 % 10;
cardnumber1 = cardnumber1 / 10;
remainder2 = remainder * 2;
if (i % 2 > 0 && remainder2 >= 10)
{
while (remainder2 > 0)
{
remainder1 = remainder2 % 10;
remainder2 = remainder2 / 10;
checksum1 = checksum1 + remainder1;
}
}
else if (i % 2 > 0)
{
checksum1 = checksum1 + remainder * 2;
}
else if (i % 2 == 0)
{
checksum2 = checksum2 + remainder;
}
}
if ((checksum1 + checksum2) % 10 != 0)
{
printf("INVALID\n");
return 0;
}
//check first two digits to determine card type//
float type;
int temp;
float truncated;
int type1;
int type2;
if (digits == 13)
{
type = cardnumber / 100000000000;
temp = type * 10;
truncated = temp / 10.0;
type1 = truncated;
type2 = truncated / 10.0;
if (type2 == 4)
{
printf("VISA\n");
}
}
else if (digits == 16)
{
type = cardnumber / 100000000000000;
temp = type * 10;
truncated = temp / 10.0;
type1 = truncated;
type2 = truncated / 10.0;
if (type1 == 51 || type1 == 52 || type1 == 53 || type1 == 54 || type1 == 55)
{
printf("MASTERCARD\n");
}
else if (type2 == 4)
{
printf("VISA\n");
}
}
else if (digits == 15)
{
type = cardnumber / 10000000000000;
temp = type * 10;
truncated = temp / 10.0;
type1 = truncated;
if (type1 == 34 || type1 == 37)
{
printf("AMEX\n");
}
}
}