Thread: Modulus 11 check digit

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    11

    Modulus 11 check digit

    I'm validating the c_code i.e the 5th digit in a string, disregarding
    the 1st character.
    example of rec is c1234576 my name my address
    The check digit has been verified and are all o.k but when
    i run the program i get error in the code. Pls help, the code is
    as below
    Code:
    void read_rec(void)
        {
        char str[107];
        char temp[6], chk_digit;
        int  weights[] = {5, 4, 3, 2};
        int  total=0, value, rem, check_num,count;
    
       while(fgets(str, 107, fp) != NULL)
         {
         strncpy(temp,str,6);
         for(count=1; temp[count] !='\0'; count++)
         putchar(temp[count]);
         fflush(stdin);
         for(count=1; count<5; count++)
          {
           value = temp[count] - '0';
           total += value * weights[count];
             }
             rem = total % MODULUS - rem;
          if( check_num < 10 )
           chk_digit = check_num + '0';
            else
             if( check_num == 10 )
              chk_digit = '0';
               else
                chk_digit = 'X';
           if(str[5] != chk_digit)
           fprintf(prn_ptr,"Error in code\n");
    
            }
             fflush(stdin);
            fprintf(prn_ptr,"\f");
                }
    thank for your responds

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    > while(fgets(str, 107, fp) != NULL)
    Why not while(fgets(str, sizeof(str), fp) != NULL)?

    > strncpy(temp,str,6);
    Why not strncpy(temp, str, sizeof(temp));?

    fflush(stdin) is something to avoid.

    Error: Undefined symbol 'fp' in function read_rec
    Error: Undefined symbol 'MODULUS' in function read_rec
    Error: Undefined symbol 'prn_ptr' in function read_rec


    Compilable code is easier to work with.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* added to make it compile */
    #define MODULUS 11
    FILE *fp = stdin;
    FILE *prn_ptr = stdout;
    
    void read_rec(void)
    {
        char str[107];
        char temp[6], chk_digit;
        int  weights[] = {5, 4, 3, 2};
        int  total=0, value, rem, check_num,count;
    
        while ( fgets(str, sizeof(str), fp) != NULL )
        {
            strncpy(temp, str, sizeof(temp));
            for ( count = 1; temp[count] != '\0'; count++ )
            {
                putchar(temp[count]);
            }
            fflush(stdin);
            for ( count = 1; count < 5; count++ )
            {
                value = temp[count] - '0';
                total += value * weights[count];
            }
            rem = total % MODULUS - rem;
    /*
    [Warning] Symbol 'rem' not initialized
    */
            if ( check_num < 10 )
    /*
    [Warning] Symbol 'check_num' not initialized
    */
            {
                chk_digit = check_num + '0';
            }
            else if ( check_num == 10 )
            {
                chk_digit = '0';
            }
            else
            {
                chk_digit = 'X';
            }
            if ( str[5] != chk_digit )
            {
                fprintf(prn_ptr,"Error in code\n");
            }
    
        }
        fflush(stdin);
        fprintf(prn_ptr,"\f");
    }
    weights[count]: weight is declared with 4 elements (0..3), count loops from 1 to 4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. error msg instead of the result so far
    By maybabier in forum C Programming
    Replies: 25
    Last Post: 03-20-2009, 02:45 PM
  3. using modulus & weighting factors
    By task in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 05:52 PM
  4. Heaps...
    By Nutshell in forum C Programming
    Replies: 14
    Last Post: 04-23-2002, 08:54 AM
  5. ISBN modulus 11 check
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2002, 01:03 PM