So I am learning C++, and I agree with others when they say guiding through a problem is better than just providing with the solution. So please bear with me.
The condition:
For a SIN to be valid it:
1 2 3 4 5 6 7 8
To obtain the weighted sum, take the digit to the left of the check
digit and then every second digit leftwards. Add each of these digits to
itself. Then, add each digit of each sum to form the weighted sum of the
even positioned digits. Add each of the remaining SIN digits (except the
check digit) to form the sum of the odd positioned digits. Add the two sums
and subtract the next highest number ending in zero from their total. If
this number is the check digit, the whole number is a valid SIN; otherwise,
the whole number is not a valid SIN.
So, example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14
SIN 193 456 787
|
check digit is 7
add first set of alternates to themselves
9 4 6 8
9 4 6 8
18 8 12 16
add the digits of each sum 1+8+8+1+2+1+6 = 27
add the other alternates 1+3+5+7 = 16
total = 43
Next highest integer multiple of 10 = 50
Difference = 7
Matches the check digit, therefore this number is a valid SIN
How would I go about implementing this? My SIN is of the type int, not an array; so I cannot simply cycle through the SIN (this cannot be changed).
> How would I go about implementing this? My SIN is of the type int, not an array;
> so I cannot simply cycle through the SIN (this cannot be changed).
Divide and conquer.
Break up the problem into small steps, each of which is simple; write a short function for each step.
Test each step and verify that it is working correctly before you move on to implement the next step.
Finally, put all these small steps together to get the desired program / high-level function.