Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <cs50.h>
- #include <math.h>
- #include <string.h>
- int c2i(char c); //prototypes for my functions
- int add_digits(int n);
- int LuhnsAlg(string CCnumber);
- int main(void)
- {
- string CCnumber; // declare a string called CCnumber
- int CCNumberLengh; // declare an int called CCNumberLengh
- do
- {
- CCnumber = get_string("Please enter a CC number: "); // Assign user input with get_string function to CCnumber
- CCNumberLengh = strlen(CCnumber); // Declare CCNumberLengh == the strlengh of the inputted CCnumber
- }
- 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)
- if (CCNumberLengh < 15 || CCNumberLengh > 16) // if CCNumberLengh is less than 15 characters or greater than 16 characters
- {
- printf("INVALID\n");
- return 0; // kill program
- }
- // Check starting numbers of the credit card & return VISA, AMEX or MASTERCARD
- // Verify CC is American Express. All AA cards start with 34 or 37
- if ((CCNumberLengh == 15) && c2i(CCnumber[0]) == 3 && (c2i(CCnumber[1]) == 4 || c2i(CCnumber[1]) == 7))
- {
- if (LuhnsAlg(CCnumber) == 0)
- {
- printf("AMEX\n");
- }
- else
- {
- printf("INVALID\n");
- }
- }
- // Verify CC is Mastercard. All MC's start with a 51, 52, 53, 54 or 55
- else if ((CCNumberLengh == 16) && (c2i(CCnumber[0]) == 5 || c2i(CCnumber[0]) == 2) && c2i(CCnumber[1]) >= 1 && c2i(CCnumber[1]) <= 5)
- {
- if (LuhnsAlg(CCnumber) == 0)
- {
- printf("MASTERCARD\n");
- }
- else
- {
- printf("INVALID\n");
- }
- }
- // Verify CC is visa. All Visa's are 13 or 16 digits long and start with the string value of '4'
- else if ((CCNumberLengh == 13 || CCNumberLengh == 16) && CCnumber[0] == '4')
- {
- if (LuhnsAlg(CCnumber) == 0)
- {
- printf("VISA\n");
- }
- else
- {
- printf("INVALID\n");
- }
- }
- else
- {
- printf("INVALID\n");
- }
- }
- int add_digits(int n) // my function to split a number like 15 and add it as literally 1 + 5
- {
- int d = 0;
- while (n > 0) // how this modulus works and why its needed for luhn's in OneNote docco
- {
- d = d + n % 10;
- n = n / 10;
- }
- return d;
- }
- int c2i(char c) // my function to convert a character to an integer value with the ascii table
- {
- // https://www.rapidtables.com/code/text/ascii-table.html
- // To convert char to int you need to subtract a 48 based offset on the ascii table
- 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)
- }
- int LuhnsAlg(string CCnumber) // my function to execute Luhn's algorith on the credit card number to check it
- {
- int i = 0; // detailed notes on how this forloop works are in OneNote docco
- int s = 0;
- int t = 0;
- 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
- {
- t = c2i(CCnumber[i]);
- 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"
- }
- 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
- {
- t = c2i(CCnumber[i]);
- s = s + t;
- }
- if (s % 10 == 0) // If the total of the last digit of this card is 0, card is valid
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement