Question: Please write your own source code and make sure that it compiles. C++ Programming. CreditCardValidator.cpp. Include some comments on the source code. Credit Card Validator
Please write your own source code and make sure that it compiles. C++ Programming.CreditCardValidator.cpp. Include some comments on the source code.
Credit Card Validator Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. In addition, cards have prefixes by type: 4 Visa 5 Mastercard 37 American Express 6 Discover In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check. The Luhn check can be described as follows. Consider the card number 4388576018402626: 1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the digits to get a single-digit number.
4 3 8 8 5 7 6 0 1 8 4 0 2 6 2 6
22=4
22=4
42=8
12=2
62=12 1+2=3
52=10 1+0=1
82=16 1+6=7
42=4
2. Now add all single-digit numbers from Step 1.
4+4+8+2+3+1+7+8=37
3. Add all digits in the odd places from right to left in the card number.
6+6+0+8+0+7+8+3=38
4. Add the results from the previous two steps.
37+38=75
5. If the result of the addition is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.
Write a program that prompts the user to enter a credit card number. Display whether the number is valid or invalid. Design your program to use the following functions:
// Return true if the card number is valid.
bool isValid(long long number)
// Get the result of Step 2.
int sumOfDoubleEvenPlace(long long number)
// Return this number if it is a single digit; otherwise, return the sum of the two digits.
int getDigit(int number)
// Return sum of odd-place digits in number.
int sumOfOddPlace(long long number)
// Return true if the digit is a prefix for this number.
bool prefixMatched(long long number, int digit)
// Return the number of digits in number
int getSize(long long number)
// Return the first numDigits digits from number. If the no. of digits
// in number is less than numDigits, return number.
long getPrefix(long long number, int numDigits)
Here are example runs of the program:
Enter a credit card number: 4388576018402626
4388576018402626 is invalid.
Enter a credit card number: 4388576018410707
4388576018410707 is valid.
Enter a credit card number: 48706286977
48706286977 is invalid.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
