Question

In: Computer Science

Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid...

Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid is shown below:We assume that there are 16 digits in a credit card number.Start from the first digit of the credit card number as in position 1; second digit as in position 2; so on until the last digit as in position 16;If a digit is in even numbered position, take the digit itself as its representative;If a digit is in odd numbered position, calculate the represetative of this digit by first doubling the digit and then, if the result exceeds 9, adding the number in ten's column and the number in one's column together. The result is the representative of this digit;Process all the 16 digits, and sum up all their representatives;If the sum is 79, or the sum is evenly divisible by 10, then the credit card number is valid; otherwise, it is not valid.Note that this is just an assignment. In real life, there are more factors to be considered to decide whether a credit card should be accepted in a transaction than the simple validity check performed by this algorithm.Your task:Design and implement a C++ program to check the validity of a credit card number.For input, your program should ask the user to enter the credit card number one digit at a time. Note that, for each digit, the user may enter characters that are not digits, then your program should discard the invalid input (for that digit) and ask the user to enter again, until it reads in a valid digit. (Hint: It would be a really good idea to implement a function to read in one valid digit. Then your main function only need to call this function 16 times for the 16 digits of a credit card number.)In the end, your program should show an appropriate message to tell the user whether the credit card number just entered is a valid one.

Solutions

Expert Solution

//------------ LuhnCheckCredCard.cpp

#include <string.h>
#include <iostream>
using namespace std;

int readDigit() {
        int n;
        do {
                cout<<" Enter digit: ";
                cin>>n;
                
                if(cin.fail() ) { // if other invalid characters entered
                        cin.clear();
                        cin.ignore();
                        cout << "\nInvalid digit. Try again" << endl;
                }
                else if (n>=0 && n<=9)    // if its between 0-9
                {       
                        return n; //return valid digit
                }
                else
                {       
                        cout << "\nEnter single digit, 0 to 9" << endl;
                }
                        
        } while(true); //repeat                 
}

int main()
{
    bool isOdd = true; // initially true, 1 2 3 ....
    int total = 0;
    int number[16];
    
    for (int i = 0; i < 16; i++)
        {
                cout<< " reading digit " << i+1;
                int n =  readDigit();
                number[i] = n;  //store digit in array
                
                if (isOdd == true)
            {
                                n = n * 2;      //double the number
                        }

        // if n becomes two digit after doubling 
        total += n / 10;        // this adds 1'st/left digit
        //if n is single digit, above statement adds 0 to total
        
        total += n % 10;        // this adds 2'nd/right digit if any
        //if n is single digit, above statement adds n to total
                
                cout<< " Total=" << total;
                isOdd = !isOdd; // toggle true / false
        }
        
        cout<< "\nYou entered: " << endl;
        for (int i = 0; i < 16; i++)
        {
                cout<< number[i]; //display all entered digits
        }
        cout<< endl;
        
        cout<< "\n Total=" << total << endl;
        
        if ( total % 10 == 0 || total==79 )  //if card is valid
        {
                cout<< "\n Credit card number is Valid..." << endl;
        }
        else
        {
                cout<< "\n Credit card number is INVALID ***" << endl;
        }
        
        
        
    return 0;
}

//------------ end of LuhnCheckCredCard.cpp

Output:


Related Solutions

Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as error in a single digit or switching two digits. The following method is used to verify actual credit card number but, for simplicity, we will describe it for numbers with 8 digits instead of 16: Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 43589795, then...
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16: • Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is...
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16: • Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is...
The following algorithm is widely used for checking whether a credit or debit card number has...
The following algorithm is widely used for checking whether a credit or debit card number has been entered correctly on a website. It doesn't guarantee that the credit card number belongs to a valid card, but it rules out numbers which are definitely not valid. Here are the steps: Check that the long number has exactly 16 digits. If not, the long number is not valid. If the long number has 16 digits, drop the last digit from the long...
write C# console app No arrays Credit card validation Problem. A credit card number must be...
write C# console app No arrays Credit card validation Problem. A credit card number must be between 13 and 16 digits. It must start with: –4 for visa cards –5 for mater cards –37 for American express cards –6 for discover cards •Consider a credit card number 4380556218442727 •Step1: double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. •Step2: now add...
The last digit of a credit card number is the check digit, which protects against transcription...
The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16: 1.) Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 4358 9795, then you...
Identify precautions for accepting the types of payments: cash, check, credit card, and debit card.   
Identify precautions for accepting the types of payments: cash, check, credit card, and debit card.   
Use attribute directives to display credit card logo based on the credit card number Use simplified...
Use attribute directives to display credit card logo based on the credit card number Use simplified rules as follows: 4 visa 5 mastercard 34 and 37 amex 30, 36, 38, 39 diners 60, 64, 65 discover
Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
Is 100202345X a valid ISBN number? If not, what would the correct check digit have to...
Is 100202345X a valid ISBN number? If not, what would the correct check digit have to be ? Solve the congruence 121x ≡ 5 mod 350.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT