In: Computer Science
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.
//------------ 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: