Question

Please write the code in C++. This is all one question with multiple requirements. 1) Create...

Please write the code in C++. This is all one question with multiple requirements.

1) Create an enumerated data type called CrdCard

The types of Credit Cards to create are all 16 digits long except American Express: American Express (34 or 37), Visa (Starts with a 4), MasterCard (Starts with 51-55), Discover (Starts with 6011).

2) Create a function that takes in the enumerated type and returns a valid credit card number using the

Luhn Algorithm (also called the Modulus 10 Algorithm:

From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

Take the sum of all the digits. If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.

Assume an example of an account number "7992739871" that will have a check digit added, making it of the form 7992739871x:

Account number

7

9

9

2

7

3

9

8

7

1

x

Double every other

7

18

9

4

7

6

9

16

7

2

x

Sum of digits

7

9

9

4

7

6

9

7

7

2

=67

The check digit (x) is obtained by computing the sum of digits then computing 9 times that value modulo 10 (in equation form, (67 * 9 mod 10)). In algorithm form:

Compute the sum of the digits (67).

Multiply by 9 (603).

The last digit, 3, is the check digit.).

Note: Length, etc... are dependent on credit card type.

genCC(CrdCard,char []), or

char *genCC(CrdCard)

3) Create a function that randomly flips only 2 digits. (Randomizes any two digits that are not the identifying number for the card carrier ie. 4 for Visa 51-55 for Mastercard etc. and not the last digit or else it will invalidate the Luhn)

void flipDig(char []);

4) Create a function that transposes only 2 digits (Randomizes two digits that are next to each other and are not the identifying number for the card carrier ie. 4 for Visa 51-55 for Mastercard etc. and not the last digit or else it will invalidate the Luhn)

5) Create a function that determines if the result is a valid Credit Card

bool validCC(char[]);

6) Loop 10,000 times and record how many valid vs. invalid Credit Cards are detected for 3) and 4)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The required code is given below in case of any doubts please ask me in comments and please do upvote.

main.cpp

#include<bits/stdc++.h>
using namespace std;
int len;
char num[16];   
enum CrdCard {AmericanExpress, Visa, MasterCard, Discover};   
char* genCC(CrdCard card){
int sum;
if(card == AmericanExpress){
len = 15;
}else{
len = 16;   
}
switch(card){
case AmericanExpress:   
num[0]=3+48;
(rand()%2)?(num[1]=4+48):(num[1]=7+48);
for(int i=2;i<len-1;i++){
num[i] = 48 + rand()%10;
}
for(int i=len-3;i>=2;i=i-2){
int n = (num[i]-'0') * 2;
if(n>9){
n = (n/10) + (n%10);
}
num[i] = n + 48;
}
for(int i=0;i<len-1;i++){
sum += num[i];
}
num[len-1] = 48 + (sum*9)%10;
break;
case Visa:
num[0]=4+48;
for(int i=1;i<len-1;i++){
num[i] = 48 + rand()%10;
}
for(int i=len-3;i>=1;i=i-2){
int n = (num[i]-'0') * 2;
if(n>9){
n = (n/10) + (n%10);
}
num[i] = n + 48;
}
for(int i=0;i<len-1;i++){
sum += num[i];
}
num[len-1] = 48 + (sum*9)%10;
break;
case MasterCard:
num[0] = 5+48;
num[1] = 48 + ((rand()%5)+1);
for(int i=2;i<len-1;i++){
num[i] = 48 + rand()%10;
}
for(int i=len-3;i>=2;i=i-2){
int n = (num[i]-'0') * 2;
if(n>9){
n = (n/10) + (n%10);
}
num[i] = n + 48;
}
for(int i=0;i<len-1;i++){
sum += num[i];
}
num[len-1] = 48 + (sum*9)%10;
break;
case Discover:
num[0] = 6+48;
num[1] = 48;
num[2] = 1+48;
num[3] = 1+48;
for(int i=4;i<len-1;i++){
num[i] = 48 + rand()%10;
}
for(int i=len-3;i>=4;i=i-2){
int n = (num[i]-'0') * 2;
if(n>9){
n = (n/10) + (n%10);
}
num[i] = n + 48;
}
for(int i=0;i<len-1;i++){
sum += num[i];
}
num[len-1] = 48 + (sum*9)%10;   
break;
}

return num;
}
void flipDig(char card[]){

}
bool validCC(char cardNo[]){
int sum = 0;
for(int i=15;i>=0;i--){
sum+=(cardNo[i]-'0');
}

return (sum % 10 == 0);
}
int main(){
srand(time(0));   
int val=0, inv=0;   
for(int i=1; i<10000;i=i+4){
int j=0;
for(int card = AmericanExpress; card <= Discover; card++){
char *a = genCC(CrdCard(card));
cout<<i+j<<". Card No: "<<a<<" Valid: ";
if(validCC(a)){
cout<<"Yes\n";
val++;
}else{
cout<<"No\n";
inv++;
}
j++;
}
}
cout<<"Total valid cards: "<<val<<endl;   
cout<<"Total invalid cards: "<<inv<<endl;   
return 0;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Please write the code in C++. This is all one question with multiple requirements. 1) Create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please do it in c++ please show steps as much as possible to help me understand...

    Please do it in c++ please show steps as much as possible to help me understand the code thank you Credit Card error detection check -> Luhn Algorithm, (http://www.freeformatter.com/credit-card-number-generator-validator.html#cardFormats) From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1...

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

    REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • Credit card numbers adhere to certain constraints. First, a valid credit card number must have between...

    Credit card numbers adhere to certain constraints. First, a valid credit card number must have between 13 and 16 digits. Second, it must start with one of a fixed number of valid prefixes : 1 • 4 for Visa • 5 for MasterCard • 37 for American Express • 6 for Discover cards In 1954, Hans Peter Luhn of IBM proposed a “checksum” algorithm for validating credit card numbers . 2 The algorithm is useful to determine whether a card...

  • Write in java . I started it can you finish it. I will rate if code...

    Write in java . I started it can you finish it. I will rate if code works What is this lab about and what will you be working on? In this lab, you will practice new tools and concepts you've learned in class and in lab. Namely this lab will be on methods, arrays (1D and 2D), and on repetition (using loops) In this lab, you will have to complete two activities. Here is what you have to do: Activity...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit 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 whether a card number is entered correctly or whether a credit card...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT