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

I need some help with a programming assignment on C++(Raspbian). We have to user

ID: 3766139 • Letter: I

Question

I need some help with a programming assignment on C++(Raspbian). We have to user Luhn's Algorithm to figure out if a credit card number is valid or not. I need to use #include <stdio.h> #include<stdlib.h> and #include<string.h>.

So first I have to ask the user for a 16 digit credit card number then I just have to tell the user if it is valid or not.

The basic steps of the algorithm is:

• Double all even-positioned digits when counting from right to left.

• Determine the sum of the undoubled digits from the products (Step 1) and each of

the unaffected (odd-positioned) digits in the original number.

• Verify the account number by determining if the sum from step 2 is a multiple of 10.

Along with the main function I have to use the following three functions:

1. readCC: Read in the 16-digit credit card number, and return it to main.

The number should be read in as a string and converted to be

stored in an integer array, which is returned from the function.

The function should also validate that the ISBN entered has the

correct number of digits before proceeding with checking its

validity. If it does not have the correct number of digits, provide

an appropriate error message and exit the program.

2. isValid: Take the credit card number as input, and check its validity,

outputting an appropriate message to the user. This function

calls the function runLuhn, which returns a checksum. If the

value returned is zero, the number is valid, if non zero, it is not

valid.

3: runLuhn Take the credit card number as input and calculate the checksum

using Luhn’s algorithm, returning its value (i.e. the remainder

obtained when the sum value calculated is divided by 10).

My program should work on the following:

MC: 5105-1051-0510-5100 Valid

MC: 5111-1111-1111-1118 Valid

MC: 5555-5555-5555-4444 Valid

VISA: 4012-8888-8888-1881 Valid

VISA: 4408-0412-3456-7890 Invalid

VISA: 4417-1234-5678-9112 Invalid

Thanks :)

Explanation / Answer

#include <iostream.h>
short getCardType();
void getCardNumber(char []);
bool checkPrefix(int, char []);
bool checkLuhn(char []);
void main()
{
short selection = getCardType();
char cardNumber[17] = "0";
getCardNumber(cardNumber);
if (checkPrefix(selection, cardNumber))
{
if (checkLuhn(cardNumber))
{
if (selection == 1)
cout << "Valid MasterCard number. ";
else
cout << "Valid VISA number. ";
}
else
{
if (selection == 1)
cout << "Invalid MasterCard number. ";
else
cout << "Invalid VISA number. ";
}
}
else
cout << "Invalid card number. ";
}
short getCardType()
{
bool validSelection = false;
short selection;
while (!validSelection)
{
cout << "Select credit card type:" << endl;
cout << "1. MasterCard" << endl;
cout << "2. VISA" << endl;
cout << "Selection: ";
cin >> selection;
if (selection == 1 || selection == 2)
validSelection = true;
else
cout << endl << "INVALID SELECTION! " << endl;
}
cout << endl;
if (selection == 1)
cout << "MasterCard chosen" << endl;
else
cout << "VISA chosen" << endl;
return selection;
}
void getCardNumber(char cardNum[])
{
cout << "Enter number (16 digits): ";
cin >> cardNum;
cout << " You entered: ";
for (int i=0; i <= 16; i++)
{
cout << cardNum[i];
if ((i % 4 == 3) && (i != 15))
cout << '-';
}
cout << endl;
}
bool checkPrefix(int selection, char cardNum[])
{
if (selection == 1)
{
if ((cardNum[0] == '5') && ((cardNum[1] == '1') ||
(cardNum[1] == '2') || (cardNum[1] == '3') ||
(cardNum[1] == '4') || (cardNum[1] == '5')))
{
cout << "Valid MasterCard prefix" << endl;
return true;
}
else
{
cout << "Invalid MasterCard prefix" << endl;
return false;
}
}
else
{
if (cardNum[0] == '4')
{
cout << "Valid VISA prefix" << endl;
return true;
}
else
{
cout << "Invalid VISA prefix" << endl;
return false;
}
}
}
bool checkLuhn(char cardNum[17])
{
short sum = 0;
int i;
for (i=0; i<8; i++)
sum += (((2*(cardNum[i*2]-48)) / 10) + ((2*(cardNum[i*2]-48)) % 10));
for (i=0; i<8; i++)
sum += (cardNum[i*2+1]-48);
if (sum % 10 == 0)
return true;
else
return false;
}