bocajbee

credit.c

Jun 1st, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 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);  //prototypes for my functions
  8. int add_digits(int n);
  9. int LuhnsAlg(string CCnumber);
  10.  
  11. int main(void)
  12. {
  13.     string CCnumber;  // declare a string called CCnumber
  14.     int CCNumberLengh;  // declare an int called CCNumberLengh
  15.  
  16.     do
  17.     {
  18.         CCnumber = get_string("Please enter a CC number: ");  // Assign user input with get_string function to CCnumber
  19.         CCNumberLengh = strlen(CCnumber);  // Declare CCNumberLengh == the strlengh of the inputted CCnumber
  20.     }
  21.     while (CCNumberLengh == 0 || c2i(CCnumber[0]) >= 9);  // do above while CCNumberLengh is == 0 OR CCnumber at index [0] is greater or equal to asci value of 57 (int)(9)
  22.  
  23.     if (CCNumberLengh < 15 || CCNumberLengh > 16)  // if CCNumberLengh is less than 15 characters or greater than 16 characters
  24.     {
  25.         printf("INVALID\n");
  26.         return 0;  // kill program
  27.     }
  28.  
  29.     // Check starting numbers of the credit card & return VISA, AMEX or MASTERCARD
  30.     // Verify CC is American Express. All AA cards start with 34 or 37
  31.     if ((CCNumberLengh == 15) && c2i(CCnumber[0]) == 3 && (c2i(CCnumber[1]) == 4 || c2i(CCnumber[1]) == 7))
  32.     {
  33.         if (LuhnsAlg(CCnumber) == 0)
  34.         {
  35.             printf("AMEX\n");
  36.         }
  37.         else
  38.         {
  39.             printf("INVALID\n");
  40.         }
  41.     }
  42.     // Verify CC is Mastercard. All MC's start with a 51, 52, 53, 54 or 55
  43.     else if ((CCNumberLengh == 16) && (c2i(CCnumber[0]) == 5 || c2i(CCnumber[0]) == 2) && c2i(CCnumber[1]) >= 1 && c2i(CCnumber[1]) <= 5)
  44.     {
  45.         if (LuhnsAlg(CCnumber) == 0)
  46.         {
  47.             printf("MASTERCARD\n");
  48.         }
  49.         else
  50.         {
  51.             printf("INVALID\n");
  52.         }
  53.     }
  54.     // Verify CC is visa. All Visa's are 13 or 16 digits long and start with the string value of '4'
  55.     else if ((CCNumberLengh == 13 || CCNumberLengh == 16) && CCnumber[0] == '4')
  56.     {
  57.         if (LuhnsAlg(CCnumber) == 0)
  58.         {
  59.             printf("VISA\n");
  60.         }
  61.         else
  62.         {
  63.             printf("INVALID\n");
  64.         }
  65.     }
  66.     else
  67.     {
  68.         printf("INVALID\n");
  69.     }
  70. }
  71.  
  72. int add_digits(int n) // my function to split a number like 15 and add it as literally 1 + 5
  73. {
  74.     int d = 0;
  75.  
  76.     while (n > 0)  // how this modulus works and why its needed for luhn's in OneNote docco
  77.     {
  78.         d = d + n % 10;
  79.         n = n / 10;
  80.     }
  81.     return d;
  82. }
  83.  
  84. int c2i(char c)  // my function to convert a character to an integer value with the ascii table
  85. {
  86.     // https://www.rapidtables.com/code/text/ascii-table.html
  87.     // To convert char to int you need to subtract a 48 based offset on the ascii table
  88.     return c - (int)('0');  // return this new value for char c into the vairable of wherther this copy is being called (in main for this program)
  89. }
  90.  
  91. int LuhnsAlg(string CCnumber)  // my function to execute Luhn's algorith on the credit card number to check it
  92. {
  93.     int i = 0;  // detailed notes on how this forloop works are in OneNote docco
  94.     int s = 0;
  95.     int t = 0;
  96.  
  97.     for (i = strlen(CCnumber) - 2; i >= 0; i -= 2)  // start counting though even indexes of digits in CC array while i > 0, then move over and check every second CC index
  98.     {
  99.         t = c2i(CCnumber[i]);
  100.         s = s + add_digits(t * 2);  // if t * 2 makes a number larger than "10" add each digit in that number together. eg 13 would become 1 + 3 = 4, thenr eturn "4" and add it to "s"
  101.     }
  102.     for (i = strlen(CCnumber) - 1; i >= 0; i -= 2)  // start counting though odd indexes of digits in CC array while i > 0, then move over and check every second CC index
  103.     {
  104.         t = c2i(CCnumber[i]);
  105.         s = s + t;
  106.     }
  107.  
  108.     if (s % 10 == 0)  // If the total of the last digit of this card is 0, card is valid
  109.     {
  110.         return 0;
  111.     }
  112.  
  113.     else
  114.     {
  115.         return 1;
  116.     }
  117. }
Add Comment
Please, Sign In to add comment