Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CC_Validator code #206

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions cc_validator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#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 CC number to validate: ";
cin >> ccNumber;

if (ccNumber == "exit")
break;

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

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

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;
}

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

cout << (doubleEvenSum % 10 == 0 ? "Valid!" : "Invalid!") << endl;

continue;
}

return 0;
}
Binary file added cc_validator.exe
Binary file not shown.