0

So I finished credit.c today and was trying out different numbers from the paypal website the course provided and got some unexpected results and don't know what to do with it. When I type in valid credit card numbers, it correctly outputs the corresponding name (AMEX, VISA, MASTERCARD) and if I input invalid ones it also outputs INVALID, all as intended. Here comes my problem though: If I input valid cc numbers from THIS link which don't correspond to the cases I defined for AMEX, VISA and MASTERCARD, it does not output invalid but nothing at all! I tried 2221000000000009 (MasterCard), 6011000990139424 (Discover) and others. Can someone help me fix this?

Here's my code:

#include <stdio.h>
#include <cs50.h>
#include <math.h>

long long get_ccn(string prompt);

int main(void)
{
int n1, n2, s1, s2;

long long cc_n = get_ccn("Enter CC: ");

long cc_n1 = cc_n;

s1 = 0;
s2 = 0;

while (cc_n > 0)
{
    //Take last digit
    n1 = cc_n % 10;
    //Add last digit to first sum
    s1 += n1;
    //Go to next digit
    cc_n -= n1;
    cc_n /= 10;

    //Take digit for second sum
    n2 = cc_n % 10;

    //Take digit * 2, if it's 10 or more, add each digit to second sum
    if ((n2 * 2) > 9)
    {
        int n2_t2 = n2 * 2;
        s2 += n2_t2 % 10;
        s2 += (n2_t2 - (n2_t2 % 10)) / 10;
    }
    else
    {
        s2 += (n2 * 2);
    }

    //Go to next digit
    cc_n -= n2;
    cc_n /= 10;
}

//Check what kind of CC it is and if it's valid
if ((s1 + s2) % 10 == 0)
{
    bool amex, mastercard, visa;

    amex = (cc_n1 >= 340000000000000 && cc_n1 < 350000000000000) || (cc_n1 >= 370000000000000 && cc_n1 < 380000000000000);

    mastercard = (cc_n1 >= 5100000000000000 && cc_n1 < 5600000000000000);

    visa = (cc_n1 >= 4000000000000 && cc_n1 < 5000000000000) || (cc_n1 >= 4000000000000000 && cc_n1 < 5000000000000000);

    if (amex)
    {
        printf("AMEX\n");
    }
    else if (mastercard)
    {
        printf("MASTERCARD\n");
    }
    else if (visa)
    {
        printf("VISA\n");
    }
}
else
{
    printf("INVALID\n");
}   

}

long long get_ccn(string prompt)
{
long long n;

do
{
    n = get_long("%s", prompt);
}
while (n < 0);

return n;
}
0

Three questions -

  1. when a credit number is invalid, as this one appears to be, what should be printed?

  2. Based on the placement of curly braces, which if statement is the else clause (the one at line 72 that contains the printf/INVALID code block) associated with? The if at line 49 or the else if at line 67?

  3. What happens if the cc number passes the checkbit test but doesn't match any of the starting number tests for a CC number?

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

| improve this answer | |
  • Thanks! Just had to add another else statement after line 69! By the way, are you some kind of programming god & do you ever sleep? ^.^ – bartman1912 Apr 24 '19 at 23:11
  • I'm sorry Dave, I cannot answer that question. :-) But seriously, here's a challenge. You've made the code work. Can you make it more efficient? – Cliff B Apr 24 '19 at 23:21
  • How's this? :) github.com/bartman1912/credit.c/blob/master/credit.c – bartman1912 Apr 24 '19 at 23:47
  • Hmmm..... Kudos for successfully implementing functions, good to learn and practice now. However, all it did was add the overhead of functions where they're only called once. (This is a red flag that a function is not needed.) Otherwise, it's all the same code. Code efficiency is presented in a couple weeks. Save this for then and give it another look. ;-) – Cliff B Apr 24 '19 at 23:59
  • Ah okay. Made some changes again, getting rid of the functions for getting the cc number and checking the type. I kept the one for the Luhn's algorithm though, since I don't need to introduce another variable for the cc number, as it gets changed during validation. Anyways thanks a lot man! – bartman1912 Apr 25 '19 at 0:22

You must log in to answer this question.

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