I have an assignment where I need to write a program to validate a credit card using the LUHN formula (MOD10). I have steps on what to do but I'm lost on the first two.
Step 1: Double the value of alternate digits of the primary account number beginning with the second digit from the right.
Step 2: Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.
I'm not sure how to go about doing either of those two steps. Also note that the 6 credit cards numbers that are being provided I'm reading in from another file. I've got that part its what to do after I get the numbers into my program where I lose it.
#include <iostream>
#include <string>
int main()
{
// get the digit at position pos
// (counting towards left with the right most digit at position zero)
const std::size_t pos = 7 ;
// from a string
const std::string numstr = "9876543210" ;
const std::size_t sz = numstr.size() ;
int digit = pos < sz ? numstr[ sz - 1 - pos ] - '0' : 0 ;
std::cout << "digit at position " << pos << " from the right is " << digit << '\n' ;
// from an unsigned long long
constunsignedlonglong number = 9876543210 ;
int divisor = 1 ;
for( std::size_t i = 0 ; i < pos ; ++i ) divisor *= 10 ;
digit = ( number / divisor ) % 10 ;
std::cout << "digit at position " << pos << " from the right is " << digit << '\n' ;
}