Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fill this in for me please. // Purpose: Write a program that prompts the user to enter a credit // card number as a long

Fill this in for me please.

// Purpose: Write a program that prompts the user to enter a credit // card number as a long integer. Display whether the number // is valid or invalid. The program processes the data until // end-of-file. Use the the Luhn check as follows: // // 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. Now add all single-digit numbers from Step 1. // // 3. Add all digits in the odd places from right to left in // the card number. // // 4. Sum the results from Step 2 and Step 3. // // 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. // // Input: Credit card numbers to validate. Will halt when EOF // is reached. "Enter a credit card number as a long integer, // Ctrl+Z, Enter to quit: " // // Output: For each credit card number, the output will output either // 's is valid. // OR // 's is invalid. // where is the cardholder's name, and // is the current credit card // number that was read in to the variable cardNumber. // //-----------------------------------------------------------------------

#include #include using namespace std;

// DO_3: Write the prototype for GetNameAndNumber and PrintGreeting.

bool isValid(long long number); int getSize(long long d); int getDigit(int digit); int sumOfDoubleEvenPlace(long long number); int sumOfOddPlace(long long number); int getPrefix(long long number, int k); bool prefixMatched(long long number, int d);

int main() { long long number; string name; // DO_4: Write a call to the function to print the greeting.

// DO_5: Write a call to the function to get the name and number. while (cin) { // DO_6: Write a call to the function isValid in the if condition. if ( ) cout << name << "'s card number " << number << " is valid." << endl << endl; else cout << name << "'s card number " << number << " is invalid." << endl << endl; // DO_7: Write a call to the function to get the name and number. } return 0; }

//--------------------------------------------------------------------- // Function displays a greeting. // params: () //--------------------------------------------------------------------- void PrintGreeting() { cout << "************************************************" << endl; cout << "* Welcome to the credit card number validator! *" << endl; cout << "************************************************" << endl; }

//--------------------------------------------------------------------- // Function gets the name and card number from the user. // params: (out, out) //--------------------------------------------------------------------- // DO_2: Complete GetNameAndNumber by filling in its header // and copying it to the prototype declaration above. // The variable number should be type long long. It should read // in the name and card number. void GetNameAndNumber( ) { cout << "Enter the name on the credit card: " << endl << "Ctrl+Z+Enter to quit: "; cin >> name; cout << endl; cout << "Enter the credit card number, " << endl << "Ctrl+Z+Enter to quit: "; cin >> number; cout << endl; }

//--------------------------------------------------------------------- // Function returns true if the card number is valid. // params: (in) //--------------------------------------------------------------------- bool isValid(long long number) { return (getSize(number) >= 13) && (getSize(number) <= 16) && (prefixMatched(number, 4) || prefixMatched(number, 5) || prefixMatched(number, 6) || prefixMatched(number, 37)) && (sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0; }

//--------------------------------------------------------------------- // Function returns the number of digits in d. // params: (in) //--------------------------------------------------------------------- int getSize(long long d) { // DO_8: Complete the body of this function. It should count the // number of digits in d. Return the number of digits. For // example, if d is a 13-digit number, it returns 13. If d is // a 5-digit number, it returns 5. }

//--------------------------------------------------------------------- // Function returns this number if it is a single digit, otherwise, // return the sum of the two digits. // params: (in) //--------------------------------------------------------------------- int getDigit(int digit) { // DO_9: Complete the body of this function. It will return the sum // of the two digits in digit. For example, if digit == 16, it // will return 7. If digit == 1, it will return 1.

}

//--------------------------------------------------------------------- // Function returns the result from Step 2 // params: (in) //--------------------------------------------------------------------- int sumOfDoubleEvenPlace(long long number) { int result = 0;

number = number / 10; // Starting from the second digit from right while (number != 0) { result += getDigit(int((number % 10) * 2)); number = number / 100; // Move to the next even place }

return result; }

//--------------------------------------------------------------------- // Function returns the sum of odd place digits in number. // params: (in) //--------------------------------------------------------------------- int sumOfOddPlace(long long number) { long result = 0;

while (number != 0) { result += (number % 10); number = number / 100; // Move two positions to the left }

return result; }

//--------------------------------------------------------------------- // Function returns the first k number of digits from number. If the // number of digits in number is less than k, return number. // params: (in) //--------------------------------------------------------------------- int getPrefix(long long number, int k) { long long result = number; int count = 0; while (count < (getSize(number) - k)) { result /= 10; count++; }

return int(result); }

//--------------------------------------------------------------------- // Function returns true if the number d is a prefix for number. // params: (in) //--------------------------------------------------------------------- bool prefixMatched(long long number, int d) { return getPrefix(number, getSize(d)) == d; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions