Credit Card Validator in C++

Published May 05, 2022

A Credit Card Validator(CCV) is a software that verifies your credit card and validates it, this program is written using C++ language. CCV helps to verify the card is a valid credit card or not.
This program is based on a simple logic that we will discuss in detail. This program uses the Luhn Algorigthm to validate a Credit Card number.

 

Features

  1. Validate your Credit Card
  2. Completely Secure
  3. Works Offline
  4. No Internet Required
  5. Faster
  6. Time Saving
  7. Reliable

 

Approach

Credit Card Number Validator, the last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16:

  1. Starting from the rightmost digit, form the sum of every alternate digit. For example, if the credit card number is 43589795, then you form the sum: 5 + 7 + 8 + 3 = 23 .
  2. Double each of the digits that were not included in the step 1 . Add all digits of the resulting numbers. For example, with the number given above, digits left out in step 1 from rightmost : 9 , 9 , 5 and 4 , doubling the digits, yields: 18 18 10 8. Adding all digits in these values yields: 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27 .
  3. Add the sums of the two preceding steps. If the last digit of the result is 0, or the sum is the multiple of 10 , then the credit card number is valid, else not valid. In our case, 23 + 27 = 50 , so the number is VALID.

 

System Requirements

  1. Processor - Intel Core Pentium or above
  2. Operating System - Windows vista or above
  3. Memory - 1Gb Ram or more
  4. Hard Disk Space - 5MB

 

Output

Credit card validator cpp

 

Credit card validator cpp2

 

Credit card validator cpp 3

 

Complete example code for Credit Carr Validator

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

bool isNumberString(const string& s) {
    int len = s.length();
    for (int i = 0; i < len; i++) {
        if (s[i] < '0' || s[i] > '9')
            return false;
    }
    return true;
}

int main() {
    string ccNumber;

    cout << "This program uses the Luhn Algorigthm to validate a CC number." << endl;
    cout << "You can enter 'exit' anytime to quit." << endl;

    while (true) {

        cout << "Please Enter a Credit Card Number to Validate: ";
        cin >> ccNumber;

        if (ccNumber == "exit")
            break;

        else if (!isNumberString(ccNumber)) {
            cout << "Bad input! ";
            continue;
        }

        int len = ccNumber.length();
        int doubleEvenSum = 0;

        // Step 1 is to double every second digit, starting from the right. If it
        // results in a two digit number, add both the digits to obtain a single
        // digit number. Finally, sum all the answers to obtain 'doubleEvenSum'.

        for (int i = len - 2; i >= 0; i = i - 2) {
            int dbl = ((ccNumber[i] - 48) * 2);
            if (dbl > 9) {
                dbl = (dbl / 10) + (dbl % 10);
            }
            doubleEvenSum += dbl;
        }

        // Step 2 is to add every odd placed digit from the right to the value

        for (int i = len - 1; i >= 0; i = i - 2) {
            doubleEvenSum += (ccNumber[i] - 48);
        }

        // Step 3 is to check if the final 'doubleEvenSum' is a multiple of 10.
        // If yes, it is a valid CC number. Otherwise, not.

        cout << (doubleEvenSum % 10 == 0 ? "VALID !" : "INVALID !") << endl;

        continue;
    }

    return 0;
}