Ryan Bury

Credit Card Validator

Validator in Action

Download Executable



Download Source Code

main.cpp

/*
	Ryan Bury
*/

#include "CardNumber.h"

int main()
{
	CardNumber myNumber;
	string str;

	cout << "Enter a credit card number wtih no spaces: " << endl;
	cin >> str;

	myNumber.setNumber(str);

	myNumber.displayNumber();

	if (myNumber.isValid())
	{
		cout << "It is a valid number." << endl;
	}
	else
	{
		cout << "It is not a valid number" << endl;
	}

	return 0;
}


CardNumber.h

/*
	Ryan Bury
*/

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

#ifndef CARDNUMBER_H
#define CARDNUMBER_H

class CardNumber
{
private:
	int *number;
	int length;
	
public:
	CardNumber();
	void setNumber(string num);
	void displayNumber();
	bool isValid();
	int addDigits(int i);
	~CardNumber();
};

#endif


CardNumber.cpp

/*
	Ryan Bury
*/

#include "CardNumber.h"

CardNumber::CardNumber()
{
	number = new int(0);
}

CardNumber::~CardNumber()
{
	delete number;
}

void CardNumber::setNumber(string num)
{
	delete number;
	length = num.length();
	if (length >= 13 && length <= 16)
	{
		number = new int[length];
		for (int i = 0; i < length; i++)
		{
			number[i] = stoi(num.substr(i, 1));
		}
	}
	else
	{
		number = new int(0);
		return;
	}
}

void CardNumber::displayNumber()
{
	cout << "Number stored: " << endl;
	for (int i = 0; i < length; i++)
	{
		cout << number[i];
	}
	cout << endl;
}

int CardNumber::addDigits(int i)
{
	int i1 = 0, i2 = 0;
	string str;
	str = to_string(i);
	if (str.length() == 1)
	{
		return i;
	}
	if (str.length() == 2)
	{
		i1 = stoi(str.substr(0, 1));
		i2 = stoi(str.substr(1, 1));
		return i1+i2;
	}

	return -1;
}

bool CardNumber::isValid()
{
	int even = 0, odd = 0, total = 0;
	int val = 0;

	//copying number array and reversing it;
	int *flipped = new int[length];
	for (int i = 0; i < length; i++)
	{
		flipped[i] = number[length - (1 + i)];
	}

	if (number == 0)
	{
		return false;
	}
	//must start with '4', '5', '6', or '37'.
	if (number[0] == 4 || number[0] == 5 || number[0] == 6 || (number[0] == 3 && number[1] == 7))
	{
		if (length == 13)
		{
			for (int i = 0; i < 6; i++)
			{
				val = flipped[i * 2 + 1];
				val = addDigits(val * 2);
				even += val;
			}
			for (int i = 0; i < 7; i++)
			{
				val = flipped[i * 2];
				odd += val;
			}
		}
		else if (length == 14)
		{
			for (int i = 0; i < 7; i++)
			{
				val = flipped[i * 2 + 1];
				val = addDigits(val * 2);
				even += val;
			}
			for (int i = 0; i < 7; i++)
			{
				val = flipped[i * 2];
				odd += val;
			}
		}
		else if (length == 15)
		{
			for (int i = 0; i < 7; i++)
			{
				val = flipped[i * 2 + 1];
				val = addDigits(val * 2);
				even += val;
			}
			for (int i = 0; i < 8; i++)
			{
				val = flipped[i * 2];
				odd += val;
			}
		}
		else if (length == 16)
		{
			for (int i = 0; i < 8; i++)
			{
				val = flipped[i * 2 + 1];
				val = addDigits(val * 2);
				even += val;
			}
			for (int i = 0; i < 8; i++)
			{
				val = flipped[i * 2];
				odd += val;
			}
		}
		total = even + odd;
		if (total % 10 == 0)
		{
			return true;
		}
	}
	else
	{
		cout << "Did not start with correct numbers" << endl;
	}
	return false;
}