|
// checksum3.cpp
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <assert.h>
#include "convert.h"
int main(int argc, char* argv[])
{
// Check digit should be 1
_tstring cnumber = _T("225-010-08");
_tcout << _T("card number: ") << cnumber << endl;
// It's too bad we had to toss out the
// const_reverse_iterator to get this to compile...
// Also, M$ will only give you the ASCII number to the
// TCHAR if: _tcout << *it;
int i = 1, c = 0;
for( _tstring::reverse_iterator it = cnumber.rbegin();
it != cnumber.rend(); it++, i++)
{
// Consume non digits
while( it != cnumber.rend() && false == isdigit( *it ) ) { it++; }
if( it == cnumber.rend() ) { break; }
if( 0 == i % 2 )
{
// Add even indexes
c += CharToNumber( *it );
}
else
{
// Cast Out Nines on the odd digits
// after multiplying by 2
int t = 2 * CharToNumber( *it );
if( t > 9 ) t -= 9;
c += t;
}
}
c %= 10;
c = 10 - c;
_tcout << _T("check digit is: ") << c << endl;
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.