0

enter image description here

I'll share the printing part but not the calculation part, though happy to do that to it's just a bit lengthy.

Nonetheless marked outputs are the right/expected output when run stand-alone but fails check50. This is regardless of the input containing or not containing spaces (it's a get_long anyway)

//get checksum
total_number_sum = first_number_sum + second_number_sum;
if(total_number_sum % 10 != 0)
{
    card_type = "INVALID";
}
else if (card_number_len == 15 && card_number_digits_forward[0] == 3 && (card_number_digits_forward[1] == 4 || card_number_digits_forward[1] == 7))
{
    card_type = "AMEX";
}
else if (card_number_len == 16 && card_number_digits_forward[0] == 5 && card_number_digits_forward[0] > 0 && card_number_digits_forward[0] <= 5) 
{
    card_type = "MASTERCARD";
}
else if ((card_number_len == 13 || card_number_len == 16) && card_number_digits_forward[0] == 4)
{
    card_type = "VISA";
}
else
{
    card_type = "INVALID";
}

printf("%s\n", card_type);

Suggestions please?

0

Can't tell for sure what all the problems are because only part of the code is posted. My best guess is that there is a problem in the checksum calculations.

Along with that, there's a problem in the check for the first two digits on the mastercard numbers. It's only checking the first digit, not the second. (Probably a cut and paste error where you didn't update the index for the array.)

Without seeing the entire code, this is the best I can offer.

I will add that it's extremely unlikely that there's a problem with check50. Are you absolutely sure that you submitted the correct version of your source code? Is it possible that you're working with multiple versions of the code and submitted the wrong one? Or maybe the source code doesn't match the compiled code?

1
  • hi Cliff, thanks for the reply and comment re: MC error that is indeed my bad. as for the make/check, the following is/are executed: ~/workspace/pset1/credit/ $ make ./credit (that runs okay, no errors) ~/workspace/pset1/credit/ $ check50 cs50/problems/2021/x/credit (which latter then fails as per above) -- I take it it's what should be?
    – nemethv
    Jan 6 at 9:12
0

Found the error (albeit admittedly still not sure why they did run okay when standalone but not in check). Human logic vs code logic. Basically I initially declared

int first_number_sum , second_number_sum , total_number_sum = 0;

and assumed all three would then get to be zero. That's not the case so it throws off the checksum counts. Declaration should be

int first_number_sum = 0, second_number_sum = 0, total_number_sum = 0;

You must log in to answer this question.

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