0

Style50 fails to even parse the code and it feels messy and cluttered. Need coding style pointers. Check50 passes. Trying to get the most out of this course, not just fumble through.

What is wrong, what is clumsy, etc?

int main(void)
{
    // asks user for a CC number
    printf("Number: ");
    long long cc_number = get_long_long();

    int total = 0; // Hans Peter Luhn algorithm result
    long long digit_one = 0; // First or second digit in cc_number at end of loop, even number of digits in cc_number means its SECOND.
    long long digit_two = 0; // If length of cc_number is even, its first digit in cc_number.
    int cc_length = 0; 

    do
    {
        // does necessary calculations relating to the last digit cc_number
        cc_length++;
        digit_one = cc_number % 10; // the last or last 2 passes of the loop get the correct first 2 cc digits
        cc_number = (cc_number - digit_one) / 10; 
        total = total + digit_one;

        // does calculation relating to second to last digit of cc_number at start of loop
        if (cc_number > 0)
        {
            cc_length++;
            digit_two = cc_number % 10;
            cc_number = (cc_number - digit_two) / 10;

            switch (digit_two)
            {
                case 0 ... 4 :
                    total = total + (digit_two * 2);
                    break;
                case 5 :
                    total++;
                    break;
                default :
                    total = total + ((digit_two * 2) % 10) + 1;
            }
        }
    } while (cc_number > 0);

    // validates the result of the HPL algorithm result
    if (total % 10 == 0)
    {
        // if validation passes, checks whether number could belong to a CC company and prints out corresponding company name
        if ((cc_length == 13 && digit_one == 4) || (cc_length == 16 && digit_two == 4))
        {
            printf("VISA\n");
        }
        else if (cc_length == 16 && digit_two == 5 && digit_one < 6 && digit_one > 0)
        {
            printf("MASTERCARD\n");
        }
        else if ((cc_length == 15 && digit_one == 3) && (digit_two == 4 || digit_two == 7))
        {
            printf("AMEX\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {   // given number is either invalid or couldn't belong to one of the 3 given CC companies
        printf("INVALID\n");
    }
}
10
  • I think style50 does not like comments in the same line as code, also I haven't seen the ... in a case before, that's a compiler-dependent extension. I'd use something like total += digit_two/5 + digit_two%5*2; instead of that switch block, but not sure how style50 would like that. – Blauelf Mar 16 '17 at 16:20
  • BTW, even if it passes check50, what's the returned card type for obviously invalid card number "2222222222"? – Blauelf Mar 16 '17 at 16:23
  • if ((cc_length == 13 && digit_one == 4) || (cc_length == 16 && digit_two == 4)) if((amount == 13 || amount == 16) && buffer[0] == 4) – masterseeker Mar 16 '17 at 16:25
  • think about what blauelf is saying that number really should be added to check50 – masterseeker Mar 16 '17 at 16:29
  • @Blauelf 2nd comment, it just quit without printing a INVALID, I fixed it. – user3096765 Mar 16 '17 at 16:34

You must log in to answer this question.

Browse other questions tagged .