Thread: Problem with modus division.

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    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
    Last edited by stillwell; 10-19-2004 at 11:41 AM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Math Division Problem
    By cookie in forum C Programming
    Replies: 7
    Last Post: 06-14-2007, 10:34 AM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Problem with division in C!!!
    By g_p in forum C Programming
    Replies: 15
    Last Post: 04-19-2007, 02:45 PM