Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++

Error Detection Based on Check Digit Schemes

Rate me:
Please Sign up or sign in to vote.
4.72/5 (27 votes)
27 Nov 2007CPOL12 min read 90.7K   2.9K   50  
A Survey of Popular Check Digit Schemes
// checksum6.cpp
//
#include "stdafx.h"

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

#include "convert.h"

int main(int argc, char* argv[])
{
    // _tstring number = _T("0823");               // c = 5
    _tstring number = _T("276616973212561");    // c = 5

    _tcout << _T("number: ") << number << endl;

    unsigned int c = 10;

    for( _tstring::iterator it  = number.begin();
                            it != number.end(); it++ )
    {
        // Consume non digits
        while( it != number.end() && false == isdigit( *it ) ) { it++; }
        if(    it == number.end() ) { break; }

        c += CharToNumber( *it );

        if( c > 10 ) { c -= 10; }

        c *= 2;

        if( c >= 11 ) { c -= 11; }
    }

    c = 11 - c;

    if( 10 == c ) { c = 0; }

    _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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions