Advertisement
bocajbee

Credit

Dec 31st, 2019
1,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4. #include <math.h>
  5. #include <string.h>
  6.  
  7. int c2i(char c);
  8. int add_digits(int n);
  9. int LuhnsAlg(string CCnumber);
  10.  
  11. int main(void)
  12. {
  13.     string CCnumber;  // declare string called CCnumber
  14.     int CCNumberLengh;  // declare int called CCNumberLengh
  15.  
  16.     do
  17.     {
  18.         CCnumber = get_string("Please enter a CC number: ");
  19.         CCNumberLengh = strlen(CCnumber);
  20.     }
  21.     while (CCNumberLengh == 0 || CCnumber[0] > 57);
  22.  
  23.     if (CCNumberLengh < 15 || CCNumberLengh > 16)
  24.     {
  25.         printf("INVALID\n");
  26.         return 0;
  27.     }
  28.     // Check starting numbers of credit cards & return VISA, AMEX or MASTERCARD
  29.     // Verify CC is American Express. AA cards start with 34 or 37. Their card lengh can be 15 digits long
  30.     if ((CCNumberLengh == 15) && c2i(CCnumber[0]) == 3 && (c2i(CCnumber[1]) == 4 || c2i(CCnumber[1]) == 7))
  31.     {
  32.         if (LuhnsAlg(CCnumber) == 0)
  33.         {
  34.             printf("AMEX\n");
  35.         }
  36.         else
  37.         {
  38.             printf("INVALID\n");
  39.         }
  40.     }
  41.     // Verify CC is Mastercard. MC's start with a 51, 52, 53, 54 or 55. Their card lengh can be 16 digits long
  42.     else if ((CCNumberLengh == 16) && (c2i(CCnumber[0]) == 5 || c2i(CCnumber[0]) == 2) && c2i(CCnumber[1]) >= 1 && c2i(CCnumber[1]) <= 5)
  43.     {
  44.         if (LuhnsAlg(CCnumber) == 0)
  45.         {
  46.             printf("MASTERCARD\n");
  47.         }
  48.         else
  49.         {
  50.             printf("INVALID\n");
  51.         }
  52.     }
  53.     // Verify CC is visa. VISA's start with a 4. Their card lengh can be 13 OR 16 digits long
  54.     else if ((CCNumberLengh == 13 || CCNumberLengh == 16) && CCnumber[0] == '4')
  55.     {
  56.         if (LuhnsAlg(CCnumber) == 0)
  57.         {
  58.             printf("VISA\n");
  59.         }
  60.         else
  61.         {
  62.             printf("INVALID\n");
  63.         }
  64.     }
  65.     else
  66.     {
  67.         printf("INVALID\n");
  68.     }
  69. }
  70.  
  71. int add_digits(int n)  // how this modulo works to add two digits together and why its needed for luhn's is in OneNote docco
  72. {
  73.     int d = 0;
  74.  
  75.     while (n > 0)
  76.     {
  77.         d = d + n % 10;
  78.         n = n / 10;
  79.     }
  80.     return d;
  81. }
  82.  
  83. int c2i(char c)  // to convert char to int you need to subtract a 48 based offset on the ascii table
  84. {
  85.     return c - (int)('0');
  86. }
  87.  
  88. int LuhnsAlg(string CCnumber)  // detailed notes on how this function & forloops work are in your OneNote docco
  89.  
  90. {
  91.     int i = 0;
  92.     int s = 0;
  93.     int t = 0;
  94.  
  95.     for (i = strlen(CCnumber) - 2; i >= 0; i -= 2)
  96.     {
  97.         t = c2i(CCnumber[i]);
  98.         s = s + add_digits(t * 2);
  99.     }
  100.     for (i = strlen(CCnumber) - 1; i >= 0; i -= 2)
  101.     {
  102.         t = c2i(CCnumber[i]);
  103.         s = s + t;
  104.     }
  105.  
  106.     if (s % 10 == 0)  // If the total of the last digit of this card leftover is 0, card is valid
  107.     {
  108.         return 0;
  109.     }
  110.  
  111.     else
  112.     {
  113.         return 1;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement