So I had this for my assignment and I have submitted it but I am having trouble compiling. Can anyone help me out ?
Credit Card Number Validation (100 Points)
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.
4388576018402626
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.
4388576018402626
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 long integer. Then the
program must display the credit card number, length of credit card number, the credit card type, and whether
the number is valid or invalid. Your program must continue to loop, processing until the file has no more
input.
Your program must implement and use the following functions (NOTE: IF YOU FAILED TO
IMPLEMENT AND USE ANY OF THESE FUNCTIONS OR FAILED TO FOLLOW THE
FOLLOWING INSTRUCTION WILL RESULT IN A SUBSTANTIAL DEDUCTION OF YOUR
GRADE):
1. int numberOfDigits(long long cardNumber)
This function must use RECURSION to find the number of digits in the cardNumber.
2. String cardType(long long cardNumber)
This function uses the first or the first two digits of the cardNumber to determine the card type.
3. int getDigit(int number)
This function should return the number if it is one digit or the sum of the digits if number is two
digit number. E.g. if the number is 8, getDigit will return 8 and on the other hand if the number
is 14, then getDigit will return 1 + 4 = 15.
4. int sumOfDoubleEvenPlace(long long cardNumber)
This function implements Step 1 and 2. When doubling every second digit, you must call the
getDigit function in case the doubling of every second digit results in two digits number.
5. int sumOfOddPlace(long long cardNumber);
This function returns the sum of odd-place digits in the cardNumber (see Step 3).
6. bool isValid(long long cardNumber);
This function returns true if the card number is valid, otherwise it must return false. This
function must call/use the following functions: must follow the following procedure:
numberOfDigits, sumOfDoubleEvenPlace, and sumOfOddPlace.
#include <cstdlib>
#include <iostream>
#include <string>
/* Handles signals such as SIGINT */
#include <signal.h>
using namespace std;
/*******************************************************************************
* This section is the class declaration
*/
class CreditCard
{
private:
/* This variable holds the credit card number */
double cardNumber;
public:
/**
* These are the method prototypes
*/
void setCardNumber(long long cardNumber);
long long getCardNumber();
int numberOfDigits(long long cardNumber);
string cardType(long long cardNumber);
int getDigit(int number);
int sumOfDoubleEvenPlace(long long cardNumber);
int sumOfOddPlace(long long cardNumber);
bool isValid(long long cardNumber);
void sigHandler(int dummy);
static void displayCreditCardInfo();
/**
* These are the prototypes for constructors
*/
CreditCard();
CreditCard(long long cardNumber);
};
/*******************************************************************************
* This section is the class implementation
*/
/**
* The default no-argument constructor
*/
CreditCard::CreditCard()
{
}
/**
* The class constructor that takes the card number as the argument
* @param cardNumber the card number
*/
CreditCard::CreditCard(long long cardNumber)
{
/* Set the credit card number */
CreditCard::cardNumber = cardNumber;
}
/**
* Method for setting the credit card number
* @param cardNumber the card number
*/
void CreditCard::setCardNumber(long long cardNumber)
{
/* Set the credit card number */
CreditCard::cardNumber = cardNumber;
}
/**
* Method for getting the card number
* @return the card number
*/
long long CreditCard::getCardNumber()
{
/* Return the credit card number */
return cardNumber;
}
/**
* Method for getting the number of digits in a card number
* @param cardNumber the card number
* @return the number of digits in the card number
*/
int CreditCard::numberOfDigits(long long cardNumber)
{
/* If the card number is less than ten return 1 */
if (cardNumber < 10)
return 1;
/* Recursively return the number of digits + 1 by passing in the quotient of the number by 10 */
return 1 + numberOfDigits(cardNumber / 10);
}
/**
* Method for getting the type of a card given the card number
* @param cardNumber the card number
* @return the card type depending on the first or first two digits of the
* card number as follows:
* -> 37 for American Express cards
* -> 4 for Visa cards
* -> 5 for Master cards
* -> 6 for Discover cards
*/
string CreditCard::cardType(long long cardNumber)
{
/* Variable num holds the card number temporarily for manipulation */
long long num = cardNumber;
/**
* While the temporary number is greater than 10 divide it by 10.
* When the number is less than 10, return this as the first digit
* in the credit card number.
*
* Switch through the first digit if 4,5 or 6 and return the respective
* card type.
*/
while (num >= 10)
num /= 10;
switch (num)
{
case 3:
num = cardNumber;
/**
* While the temporary number is greater than 100 divide it by 100.
* When the number is less than 100, return this as the first two
* digits in the credit card number.
*
* Check the number formed by the first two digits if 37 and
* return the respective card type.
*/
while (num >= 100)
{
num /= 100;
}
if (num == 37)
return "AMERICAN EXPRESS";
else
return "";
case 4:
return "VISA";
case 5:
return "MASTER";
case 6:
return "DISCOVER";
default:
return "";
}
}
/**
* Method for getting the required single digit number.
* @param number The number to be returned if it is one digit or whose digits
* are to be summed if it is greater than 9
* @return The required single digit number
*/
int CreditCard::getDigit(int number)
{
/* Return the sum of the two digits forming the number if it is two-digit */
if (number > 9 && number < 100)
return number % 10 + number / 10;
/* Return the digit forming the number if it is one-digit */
else if (number >= 0 && number <= 9)
return number;
}
/**
* Method for getting the sum of double digits in even positions from right
* to left in a card number
* @param cardNumber the card number
* @return the sum of double digits in even positions from right to left in a
* card number
*/
int CreditCard::sumOfDoubleEvenPlace(long long cardNumber)
{
/**
* Variable number holds the string representation of the card number
* for manipulation
*/
string number = to_string(cardNumber);
/**
* Variable double_evens_sum holds the sum from right to left of digits in
* the even places
*/
int double_evens_sum = 0;
/**
* Find the sum of the double of digits at even positions of the credit
* card number from right to left
*/
for (int i = number.length() - 2; i >= 0; i -= 2)
{
double_evens_sum += getDigit((number[i] - '0') * 2);
}
/* Return this sum */
return double_evens_sum;
}
/**
* Method for getting the sum of digits in odd positions from right to left
* in a card number
* @param cardNumber the card number
* @return the sum of digits in odd positions from right to left in the card
* number
*/
int CreditCard::sumOfOddPlace(long long cardNumber)
{
/**
* Variable number holds the string representation of the card number
* for manipulation
*/
string number = to_string(cardNumber);
/**
* Variable odds_sum holds the sum from right to left of digits in the
* odd places
*/
int odds_sum = 0;
/**
* Find the sum of digits at odd positions of the credit card number from
* right to left
*/
for (int i = number.length() - 1; i >= 0; i -= 2)
{
odds_sum += (number[i] - '0');
}
/* Return this sum */
return odds_sum;
}
/**
* Method for determining if the card is valid or not
* @param cardNumber the card number
* @return the boolean value whether is valid or not
*/
bool CreditCard::isValid(long long cardNumber)
{
/**
* The number of digits in a valid credit card number is between 13 and 16.
*
* If the sum of digits at odd positions from right to left of the
* credit card number and double sum of digits at even positions from
* right to left of the credit card number is a multiple of 10 then the
* card is valid but if the sum is not a multiple of 10 then the card is
* invalid.
*/
if (numberOfDigits(cardNumber) >= 13 && numberOfDigits(cardNumber) <= 16
&& (sumOfDoubleEvenPlace(cardNumber) + sumOfOddPlace(cardNumber)) % 10 == 0)
{
return true;
} else
{
return false;
}
}
/**
* Method for displaying the credit card information
* Using this valid sample card number: 4388576018402626
* Using this invalid sample card number: 4388576018410707
* @param creditCard
*/
static void displayCreditCardInfo(CreditCard creditCard)
{
/**
* Variable temp_num holds the card number temporarily after it has been
* entered from the stdin
*/
long long temp_num;
/**
* Variable choice holds the user's choice to test the validity of the next
* credit card number or not. Choice 1 terminates the program, 2 asks for
* next input.
*/
int choice = 2;
/**
* While the user opts to enter the next credit card number(choice 2)
*/
while (choice == 2)
{
/**
* Asking and accepting user input of the credit card number
*/
cout << "\nPlease enter the credit card number: ";
cin >> temp_num;
creditCard.setCardNumber(temp_num);
/**
* Displaying information about the credit card number.
* The information includes the
* -> number read,
* -> the number of digits in the credit card number,
* -> the credit card type and
* -> whether the credit card is valid or invalid.
*/
cout << "\nCredit Card Number: " << creditCard.getCardNumber() << "\n";
cout << "Number of Digits: " << creditCard.numberOfDigits(creditCard.getCardNumber()) << "\n";
cout << "Credit Card Type: " << creditCard.cardType(creditCard.getCardNumber()) << "\n";
cout << "Valid? \t " << (true == creditCard.isValid(creditCard.getCardNumber()) ? "YES" : "NO") << "\n";
/**
* Asking and accepting user input of their choice to terminate the
* program or scan the next credit card number.
*/
cout << "\nWould you like to \n\t 1) quit or \n\t 2) process another card number?\n\t >> ";
cin >> choice;
}
}
/**
* Method to override the default action of a signal and respond otherwise
* instead. The signal in this case is SIGINT.
* @param dummy the signal number
*/
void sigHandler(int dummy)
{
/*******************************************************************************
* This section is the entry point to the program
*/
/**
* The main method; entry point of the program
* @param argc
* @param argv
* @return
*/
int main(int argc, char** argv)
{
/**
* Defer control to sigHandler to prevent ctrl+c default action
*/
signal(SIGINT, sigHandler);
/**
* Welcome the user to the program.
*/
cout << "\n\n "
"****************************************************************\n"
"\t\t\tWelcome to assignment2\n "
"****************************************************************\n\n";
/**
* Create the credit card object
*/
CreditCard creditCard;
/**
* Call displayCreditCardInfo method for displaying information about the
* credit card number.
*/
displayCreditCardInfo(creditCard);
/**
* Inform the user that the program is terminating.
*/
cout << "\n\n "
"****************************************************************\n"
"\t\tProgram terminated. Exiting gracefully\n "
"****************************************************************\n\n";
/**
* Terminate the program gracefully.
*/
exit(EXIT_SUCCESS);
Reading the assignment, I don't think the prof meant for you to create a class, just a bunch of functions.
Nice job on the comments! They indicate what you're trying to do, which is great. In a real-world setting you won' t need quite as many, but you've got the hang of what to say.
Thanks.I installed a standalone compiler (code blocks) and it compiled for me but still having issues with compiling it in visual studio for some reason.might me my anti virus