1

I am currently trying my best to solve the question. However, I have encountered the following problem:

After checking the sum, I want to verify the first 2 digits from the card no. so I use the following method:

int main(void)
{
  long long ccn;
  do
  {
    ccn = get_long_long("Credit Card No.:\n");
  }
  while (ccn < 0);
  int ccn_len;
  long long count = ccn;
  long long bccn = ccn;
  for (ccn_len = 0; count != 0; ccn_len++, count /= 10);
    int sum = 0; //checksum
    for (int i = 0; i < ccn_len; ccn /= 10, i++) 
    {
      if (i % 2 == 0)
      {
        sum += ccn % 10;
      }
      else
      {
        int digit = (ccn % 10) * 2;
        sum += digit / 10 + digit % 10;
      }
    }
    if (sum % 10 != 0) 
    {
      printf("INVALID");
    }
    else
    {
      int a = bccn / 1e13;
      if ((bccn / 1e13 == 34 || bccn / 1e13 == 37) && ccn_len == 15)
      {
        printf("AMERICAN EXPRESS");
      }
      else if (bccn / 1e12 == 4 && ccn_len == 13)
      {
        printf("VISA");
      }
      else if (ccn_len == 16)
      {
        if (bccn / 1e15 == 4)
        {
          printf("VISA");
        }
        if (bccn / 1e14 > 50 || bccn / 1e14 < 56)
        {
          printf("MASTERCARD");
        }
      }
      else
      {
        printf("INVALID");
      }
    }
  }
}

Let's say it is a valid AE card: 378282246310005 with a length of 15 digits

In the above code, I use ccn / 1e13 to get the first two digits to check whether it is 34 or 37.

However, after satisfying the checksum, the output still shows INVALID.

I try to use another method, I set a variable a and a = ccn / 1e13 and then I put a in the if-statement:

if ((a == 34 || a == 37) || ccn_len == 15)

everything works fine this time.

Can anyone tell me what is going wrong with my code? Or how do I write better?

Your replies are very much appreciated.

augarte
  • 57
  • 2
  • 8
heihei
  • 101
  • 8
  • please provide complete code, with variable declarations,initializations, etc... – Pablo Yaggi Aug 02 '20 at 14:51
  • There is another question i want to ask, you see in the above code, i have created two more variables bccn and count, which they are essentially equal to ccn (credit card no.). because if i don't do it, after go through a few line of codes, the original value of ccn might have changed, is there a way that i can keep the original value of ccn unchaged, while i can use it in calculations? – heihei Aug 02 '20 at 15:21

1 Answers1

0

1e13 is a floating-point constant with type double. In bccn / 1e13, bccn is converted to double, and then floating-point division is performed, yielding a number such as 37.82822463100050214279690408147871494293212890625. Then bccn / 1e13 == 37 evaluates as false because 37.82822463100050214279690408147871494293212890625 is not equal to 37.

Rewrite your code to use only integer arithmetic (do not use floating-point constants like 1e13) or to treat credit card “numbers” as strings of digits rather than as integers.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • You are the best! I went to my code and change the line of code from: bccn / 1e13 ==37, to bccn / (long) 1e13 ==37. Now everything is okay. btw i have another question, i would be very grateful if you could help too. You see in the above code, i have created two more variables bccn and count, which they are essentially equal to ccn (credit card no.). because if i don't do that, after go through a few line of codes, the original value of ccn might have changed, is there a way that i can keep the original value of ccn unchaged, while i can use it in calculations? – heihei Aug 03 '20 at 05:57