Thread: I cant find what is wrong with this code

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

    I cant find what is wrong with this code

    This program is to read record from a file and then validate
    the 5th element str[5].
    From the valid check digits out of 45 recs only 5 is reported
    valid. I dont know what is wrong with this code

    Example of rec on file is c12543256 my name where i leave......\n
    please help, am i reading the record wrongly?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>    
    
        char str[107];
        char temp[5], chk_digit, *c_ptr;
        int  weights[] = {5, 4, 3, 2}, *w_ptr;
        int  total=0, value, rem, check_num;
    
       while(fgets(str,sizeof(str), fp) != NULL)
         {
       w_ptr = weights;
         strncpy(temp,&str[1],4);
    
       for(c_ptr=temp; *c_ptr; c_ptr++)
          {
           value = *c_ptr - '0';
           total += value * *w_ptr++;
             }
    
             rem = total % MODULUS;
             check_num = MODULUS - rem;
          if( check_num < 10 )
           chk_digit = check_num + '0';
            else
             if( check_num == 10 )
              chk_digit = '0';
               else
                chk_digit = 'X';
    
           if(chk_digit != str[5])
           {
           fprintf(prn_ptr,"\nError in code");
             }
          else
          fputs(str,fv);
            }
    
          fprintf(prn_ptr,"\f");
                }

    thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: I cant find what is wrong with this code

    Originally posted by senegene
    This program is to read record from a file and then validate
    the 5th element str[5].
    Don't know if this is your problem or not, but the 5th element of the array is str[4] not str[5]. Remember they are accessed from 0-n, not 1-n.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  2. What's wrong with this code?
    By Finchie_88 in forum Networking/Device Communication
    Replies: 10
    Last Post: 05-27-2005, 09:46 AM
  3. What's wrong with my Win32 Wrapper code?
    By miica in forum Windows Programming
    Replies: 9
    Last Post: 02-22-2005, 08:55 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. what's wrong with this qsort code?
    By Sargnagel in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 06:58 AM