# Thread: Problem with modus division.

1. ## Problem with modus division.

I have made this program to take in the first nine digits of a population registration number, and calculate the 10th.

The program works fine, as long as the modulus 11(method for the calculation) calculation do not return 121. In that case, the modus division returns 11....which shouldn't be possible, as it's being divided by 11, and is supposed to return 0.

Please take a look, and see if you can locate the problem.
Code:
```#include <vcl.h>
#include <iostream>
#include <stdlib.h>
#pragma hdrstop
using namespace std;

//---------------------------------------------------------------------------

#pragma argsused

int main()
{
int Imod1, Imod2, Imod3, Imod4, Imod5,
Imod6, Imod7, Imod8, Imod9, Imod10, moddy;

char mod1[2], mod2[2], mod3[2], mod4[2], mod5[2],
mod6[2], mod7[2], mod8[2], mod9[2], mod10[2], input[12];

char* i = input;

cout << "Indtast cprnr. der skal udregnes ";
cin  >> input;

strncpy(mod1, i, 1);
i += 1;
strncpy(mod2, i, 1);
i += 1;
strncpy(mod3, i, 1);
i += 1;
strncpy(mod4, i, 1);
i += 1;
strncpy(mod5, i, 1);
i += 1;
strncpy(mod6, i, 1);
i += 2;
strncpy(mod7, i, 1);
i += 1;
strncpy(mod8, i, 1);
i += 1;
strncpy(mod9, i, 1);

Imod1 = atoi(mod1);
Imod2 = atoi(mod2);
Imod3 = atoi(mod3);
Imod4 = atoi(mod4);
Imod5 = atoi(mod5);
Imod6 = atoi(mod6);
Imod7 = atoi(mod7);
Imod8 = atoi(mod8);
Imod9 = atoi(mod9);

moddy = (Imod1*4) + (Imod2*3) + (Imod3*2) + (Imod4*7) +
(Imod5*6) + (Imod6*5) + (Imod7*4) + (Imod8*3) + (Imod9*2);//modulus 11 calculation

Imod10 = moddy%11; //modus division

//cout << endl << moddy << endl;

Imod10 = 11 - Imod10; //could this line be the problem?

//cout << Imod10;

cout << "\n\nDit cprnr. er: " << Imod1 << Imod2 << Imod3 << Imod4
<< Imod5 << Imod6 << "-" << Imod7 << Imod8 << Imod9 << Imod10;

cin  >> moddy;

return 0;
}```
Hehe, sorry for keep editing this. I thought I located the problem, but I didn't

2. manipulate the components of Imod10 such that Imod10 == 121. Uncomment the cout lines below. Monitor the value of Imod10 after each statement. Congratulate yourself for debugging your own program.

Imod10 = moddy%11; //modus division

//cout << endl << moddy << endl;

Imod10 = 11 - Imod10; //could this line be the problem?

//cout << Imod10;

3. made a some small changes and added some more output. I think you'll see where the problem is:

Code:
```#include <iostream>

using namespace std;

int main()
{
int Imod1, Imod2, Imod3, Imod4, Imod5,
Imod6, Imod7, Imod8, Imod9, Imod10, moddy;

char input[12];

char* i = input;

cout << "Indtast cprnr. der skal udregnes ";
cin  >> input;

Imod1 = *i - '0';
i++;
Imod2 = *i - '0';
i++;
Imod3 = *i - '0';
i++;
Imod4 = *i - '0';
i++;
Imod5 = *i - '0';
i++;
Imod6 = *i - '0';
i++;
Imod7 = *i - '0';
i++;
Imod8 = *i - '0';
i++;
Imod9 = *i - '0';

moddy = (Imod1*4) + (Imod2*3) + (Imod3*2) + (Imod4*7) +
(Imod5*6) + (Imod6*5) + (Imod7*4) + (Imod8*3) + (Imod9*2);//modulus 11 calculation

cout << "moddy = " << moddy << endl;
cout << "changing moddy to 121" << endl;
moddy = 121;
Imod10 = moddy%11; //modus division

cout << "Imod10 = " << Imod10 << endl;

Imod10 = 11 - Imod10; //could this line be the problem?

cout << "Imod10 = " << Imod10 << endl;

cout << "\n\nDit cprnr. er: " << Imod1 << Imod2 << Imod3 << Imod4
<< Imod5 << Imod6 << "-" << Imod7 << Imod8 << Imod9 << Imod10 << endl;

return 0;
}```
Indtast cprnr. der skal udregnes 1234567890
moddy = 174
changing moddy to 121
Imod10 = 0
Imod10 = 11

Dit cprnr. er: 123456-78911

4. Well none of your strncpy() calls guarantee that a \0 is placed at the end of the string
So all your atoi() calls are roaming off through memory producing garbage results.

> int Imod1, Imod2, Imod3, Imod4, Imod5,
> Imod6, Imod7, Imod8, Imod9, Imod10
Hint - use an array!