0

The 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 <stdio.h>
 #include <cs50.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <string.h>
 #include <math.h>

 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
2
  • Have you tried running and testing the code yourself before submitting it to check50. Remember check50 does not exist in the real life, and If you did run your program what results did you get ? – A.Emad Dec 25 '16 at 20:21
  • Yes, I got no errors. But when I myself test it I don't get the right outputs (i.e., I get VISA instead of AMEX with a cc number of 378282246310005). I – KhalidMukadam Dec 25 '16 at 20:24
-1

The specification says multiply every other digit by 2. it seems to me you are multiplying each digit. You have to skip the first digit, multiply the second by two, skip the third, multiply the forth by 2 and so on, then you add all these results together. Moreover,I think you are missing something important in addition. You are asking for the char name[a]; remember that this is a type char with value '3' and equivalent to the integer 51.

For Example let's say that

word = "378282246310005" According to your program word is a string, or an array of character

when you for example do the following operation:

(name[0] - 48) * 2

your expecting name[0] to return 3, since the first character in the string is 3; however that is not the case. name[0] will return 51 because according to Ascii chart the character '3' is equivalent to the number 51. So basically, you are doing (51 - 48 ) * 2, instead of (3 - 48) * 2

I think those are the major problems you have.

So my suggestions to solve the two problems:

you can add a condition in the for loops:

int prodig = 0;
if(i % 2 != 0)
{
    prodig = ("odd digit"-48)* 2;
}

for getting the equivalent digit

There are many ways to do so, however my suggestion will be doing it by using atoi function, which takes a string and returns it as an int

you can do this in your code: int x = atoi(name); instead of using name you can get the current digit by doing something similar to this:

int digit = (x / pow(10, length - i - 1)) % 10;

in the first iteration for x = 378282246310005, digit should be equals 3 then 7 on the next iteration and so on.

Feel free to ask if you need further clarification and if this answers your question accept my answer

1
  • This would be the second to first digit, not the second to last digit. – Evan Carroll Jan 29 '19 at 6:39

You must log in to answer this question.

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