Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Credit card numbers follow certain patterns. A credit card number must have

ID: 3559400 • Letter: C

Question

C++

Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with:
4 for Visa cards
5 for Master cards
37 for American Express cards
6 for Discover cards

In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine if a card number is entered correctly or if a credit card is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, 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 two digits to get a single-digit number.
2 * 2 = 4
2 * 2 = 4
4 * 2 = 8
1 * 2 = 2
6 * 2 = 12 (1 + 2 = 3)
5 * 2 = 10 (1 + 0 = 1)
8 * 2 = 16 (1 + 6 = 7)
4 * 2 = 8

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. Sum the results from Step 2 and Step 3.
37 + 38 = 75

5. If the result from Step 4 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 as a long integer. Display the credit card number, length of credit card number and whether the number is valid or invalid. There will be two versions of your code in your program (execute a version based on user input).

Must use methods/functions to perform the steps starting with a high level method/functions names

Explanation / Answer

#include<sstream>
#include<string>
using namespace std;
bool creditCardType(string cardNumber)
{
return (cardNumber.compare(0, 1, "4") == 0) || // TYPE VISA.
       (cardNumber.compare(0, 1, "5") == 0) || // TYPE MASTER.
       (cardNumber.compare(0, 2, "37") == 0) || // TYPE AMERICAN EXPRESS
       (cardNumber.compare(0, 1, "6") == 0); // TYPE DISCOVER.   
}
int getDigit(int number)
{
int result =0;
while(number)
{
result = result + (number%10);
number = number/10;
}
return result;
}
// to calculate sum of numbers at even places.
int sumOfDoubleEvenPlaces(const string& cardNumber)
{
int sum_even_places = 0;
for (int i = cardNumber.length()-2; i>=0; i-=2)
{
sum_even_places = sum_even_places + getDigit(2*(cardNumber[i] - '0'));
}
return sum_even_places;
}
int sumOfOddPlace(const string& cardNumber)
{
int sum_odd_places = 0;
for (int i = cardNumber.length()-1; i>=0; i-=2)
{
sum_odd_places = sum_odd_places +((cardNumber[i] - '0'));
}
return sum_odd_places;
}
// function to check card is valid or not.
bool isValid(const string& cardNumber)
{
return ((sumOfDoubleEvenPlaces(cardNumber)+sumOfOddPlace(cardNumber))%10==0);
}
//Write a program that prompts the user to enter a credit card number as a long integer.
int main()
{
long int cardnNum;
int choice;
cout << "Enter your credit card number ";
cin >>cardnNum;
cout << endl;
stringstream ss;
ss << cardnNum;
string cardnumber = ss.str();
// You program will begin by asking the user what type of execution:
// 1= use asset logic on length of credit card number
// 2= print error message and continue on to next credit card number.
//Display the credit card number, length of credit card number and whether the number is valid or invalid.
cout <<"Enter 1 to use asset logic on length of credit card number" << endl;
cout <<"Enter 2 to use creditCardType logic on credit card number " << endl;
cin >> choice;
cout << endl;
if(choice == 1)
{
cout << cardnumber << " has Type ";
if(cardnumber[0] == '4') cout <<" VISA";
if(cardnumber[0] == '5') cout <<" MASTER";
if(cardnumber[0] == '6') cout <<" DISCOVER";
if(cardnumber[0] == '3') cout <<" AMERICAN EXPRESS";
cout << endl;
if((cardnumber.length()>=13 && cardnumber.length()<=16))
cout << cardnumber << " has length " <<cardnumber.length() <<" Is Valid card " << endl;
else
cout << cardnumber << " has length " <<cardnumber.length() <<" Is InValid card " << endl;
}
else if(choice == 2)
{
if(creditCardType(cardnumber) && isValid(cardnumber))
{
cout << cardnumber << " has Type ";
if(cardnumber[0] == '4') cout <<" VISA";
if(cardnumber[0] == '5') cout <<" MASTER";
if(cardnumber[0] == '6') cout <<" DISCOVER";
if(cardnumber[0] == '3') cout <<" AMERICAN EXPRESS";
cout <<" Is Valid card " << endl;
}
cout << cardnumber << " has Invalid Type.so "<< cardnumber<<" Is InValid card " << endl;
}
system("pause");
return 0;
}