0

this is the message

 identifies 4111111111111113 as INVALID
expected EOF, not "INVALID"

this is the code

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

bool checksum(string credit);

int main()
{
long long cardnumber = 0;
long long calc[1];
string credit;
string lengthkey[1];

//user input
do
{
    credit = get_string("Card number: ");
}
while ((credit <= 0));
//to check if condition works
//printf("%s credit\n", credit);


//the long long version of user's string input
//cardnumber = strtoll(credit, NULL, 10);

//copy of long long for calculation
///calc[0] = cardnumber;
//string for address and length
lengthkey[0] = credit;
//printf("%s length\n", lengthkey[0]);

if ((strlen(lengthkey[0]) == 15) || (strlen(lengthkey[0]) == 16) || (strlen(lengthkey[0]) == 13))
{//amex
    if (strlen(lengthkey[0]) == 15)
    {
        if ((lengthkey[0][0] - '0') == 3 && ((lengthkey[0][1] - '0') == 4 || (lengthkey[0][1] - '0') == 7))
        {
            if(checksum(credit))
                printf("AMEX\n");
        }
        else
            printf("INVALID\n");
    }
    //master card
    if (strlen(lengthkey[0]) == 16) 
    {
        if ((lengthkey[0][0] - '0') == 5 && ((lengthkey[0][1] - '0') == 1 || (lengthkey[0][1] - '0') == 2 || (lengthkey[0][1] - '0') == 3 || (lengthkey[0][1] - '0') == 4 || (lengthkey[0][1] - '0') == 5))
        {
            if(checksum(credit))
                printf("MASTERCARD\n");
        }
        else
            printf("INVALID\n");
    }
    //visa
    if (strlen(lengthkey[0]) == 13 || strlen(lengthkey[0]) == 16)
    {
        if ((lengthkey[0][0] - '0') == 4)
        {
            if(checksum(credit))
                printf("VISA\n");
            else
            printf("INVALID\n");
        }
    }
}
//invalid
else
{
    printf("INVALID\n");
    //369421438430814, 5673598276138003, 4111111111111113,  4222222222223
}
}

//luhn's algorithm
bool checksum (string credit)
{
//calc[0] = cardnumber;
string checksumarray[1];
checksumarray[0]= credit;
long long  mult, remaining, firstnumsum = 0, sum_of_even = 0,  finalsum;
int  a = strlen(checksumarray[0]);
//sum the odd digits
for (int x = a - 1; x >= 0; x -= 2)
{
    firstnumsum = firstnumsum + (checksumarray[0][x] - '0');
}
//sum of even digits
for (int j = a - 2; j >= 0; j -=2)
{
    mult = 2 * (checksumarray[0][j] - '0');
    for (; mult > 0;  )
    {
        remaining = mult % 10;
        sum_of_even += remaining;
        mult /= 10;
    }
}
finalsum = firstnumsum + sum_of_even;
if (finalsum % 10 == 0)
    return true;
else
    return false;
}
5
  • Can't tell what's up. Too much of the code wasn't posted, particularly var and array declarations.
    – Cliff B
    Commented Jul 16, 2020 at 1:06
  • I've edited it now, is it better Commented Jul 16, 2020 at 1:15
  • No, it's worse. THat's even less code.
    – Cliff B
    Commented Jul 16, 2020 at 1:24
  • sorry, i read too much of the code was posted Commented Jul 16, 2020 at 1:27
  • I've edited it again Commented Jul 16, 2020 at 1:31

1 Answer 1

5

When the code is run with the test data that check50 uses, 4111111111111113, it prints INVALID twice. check50 was expecting the program to end, not to print the second INVALID.

The problem is obvious with a little testing, but I'll give you a hint. Go into the code and change every line that prints "INVALID". Add a unique number to each printf statement so that you can see WHICH printf is being executed.

Once you rebuild the code and run it with the test data above, you should have no problem figuring out what's happening.

Tip for the future: When check50 fails, always go and look at the link in the output. It gives more details. Usually, it will display exactly what output it expected and what it got.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Alright thanks, I'll try that now Commented Jul 16, 2020 at 1:41
  • It worked even for my janky code! Thank you so much for the useful tip.
    – Kennster
    Commented Apr 14, 2023 at 23:07

You must log in to answer this question.

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