-4

I am working on the credit problem of CS50. However, I am only printing INVALID no matter what card number I put in. May I ask what is the problem with my code? It seems that there is something wrong with the part to calculate the total sum.

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

    int main(void)
    {
        // Get the card number
        long num;
        do 
        {
            num = get_long("What is the card number?\n");
        } while (num < 0);

        long sum = 0, sum2 = 0, count = 0;
        //Get the sum
        for (long i = num; i > 0; i = i / 10)
        {
            sum += i % 10;
            count++;
        }
        for (long i = num / 10; i > 0; i = i / 100)
        {
            sum2 += i % 10;    
        }

        if ((sum + sum2) % 10 != 0) 
        {
            printf("INVALID");
        }
        else 
        {
            long digits = num / (10 * (count - 2));
            if (count == 15 && 
               (digits == 34 || digits == 37)) 
            {
                printf("AMERICAN EXPRESS");
            } 
            else if (count == 16 && 51 <= digits <=55)
            {
                printf("MASTERCARD");
            }
            else if ((count == 16 || count == 13) && (digits / 10) == 4)
            {
                printf("VISA");               
            }
            else 
            {
                printf("INVALID");
            }

        }
    }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mkmkmkmk
  • 47
  • 1
  • 6
  • 2
    It isn't helpful to refer to CS50, since almost none of us probably go to your school. I'm guessing that you're trying to implement the Luhn algorithm, but you don't explain that. You also don't have any comments explaining your algorithm, so we can't verify that the code meets the intended logic because you haven't presented the intended logic. Lastly, you rely on `cs50.h` and some external functions like `get_long()`. Please show a minimal and complete problem. – torstenvl May 18 '19 at 15:47
  • Related: https://stackoverflow.com/q/20907265/694576 – alk May 18 '19 at 15:56
  • @torstenvl: It may be that you are unable to help if the question is tagged [tag:cs50] because you don't have the relevant knowledge, but the tag is (very) useful. It explains immediately a number of idiosyncrasies in the code which the students of the course are compelled (induced?) into using. There's also the [CS50 Stack Exchange](https://cs50.stackexchange.com/) web site. (No; the question is not automatically off-topic here because it is associated with CS50.) – Jonathan Leffler May 18 '19 at 16:27
  • Are you using a 64-bit compiler? If not, `long` may not be long enough. Have you printed the card number returned by `get_long()`? – Jonathan Leffler May 18 '19 at 16:28

1 Answers1

1

This gives the sum of every digit.

for (long i = num; i > 0; i = i / 10)
        {
            sum += i % 10;
            count++;
        }

Review the problem set again, specifically the discussion of Luhn's Algorithm. There are two sums, however they are "mutually exclusive". If a digit is added to sum1 it will not be added to sum2. And versa vice. There is also a noticeable lack of *2 (times 2) anywhere in the code.

Perhaps you will find this walkthrough video helpful. Be warned: it is from an earlier version of the course and mentions the function get_long_long. That has been retired, use get_long as you have done here.

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15
  • What I did was that I first added every digit (giving the sum); then I calculated sum2, which adds every second digit. Then I have sum (odd numbered digits + even number digits) + sum2 (even number digits) to get the total sum (odd numbered digits + 2 * even number digits). i guess that works as well. – mkmkmkmk May 19 '19 at 17:28
  • It does not work as well because of this little detail: "Multiply every other digit by 2, starting with the number’s second-to-last digit, and then **add those products' digits together**." – DinoCoderSaurus May 19 '19 at 20:55