Thread: check digit

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    30

    check digit

    i'm trying to make a check digit for my program as below

    The check digit sometimes must have a value of 10, and this is indicated by putting an X in the check-digit location.

    I know it has something to do with the ASCII table that X has a value of 88 and I need to work with that somehow.

    How do I read if a user has entered an X and then set the X to a value of 10?
    Last edited by Jailan; 09-14-2007 at 07:31 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, so where is your "program as below" ?
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    Sorry I meant "as below" as in what I wanted to make as the check digit.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > How do I read if a user has entered an X and then set the X to a value of 10?
    Since your question is, um... unclear

    The answer could be:
    Code:
    int X = 0;
    if(getchar() == 'X')
        X = 10;
    But it's probably not

    your question sounds sortof, homeworky.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh, you mean you were referring to another message on the forum, which has since changed the order since your post, and the idea of "below" doesn't mean anything anymore.

    Post your own effort.
    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.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    This is the brief:

    The ISBN has ten digits. These are normally broken into smaller groups with hyphens or spaces to make it easier to transcribe them. The break-up varies, because the publisher definition can be done in several ways, depending on the size of the publisher.

    Some examples of ISBN’s are:
    0-7897-0414-5
    0-262-68053-X
    0 201 54322 2

    Notes:
    1) There is no guarantee that there will always be four numeric groups, so you should consider that the number of characters may be any value of ten or greater.
    2) The check digit sometimes must have a value of 10, and this is indicated by putting an X in the check-digit location.
    3) The

    Your program will accept a line of input containing an ISBN with dashes or spaces, identify the digits, and perform the above calculation. Note that the digits in the input string will be characters, with their associated ASCII value. To convert a single digit character to its corresponding internal integer value for calculation purposes, simply subtract the character code for '0'.

    E.g.
    char c = ‘9’;
    int num = c – ‘0’;
    Will put the value 9 into num.

    And this is what I've done:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int validate(char ISBN[]);
    
    int main(int argc, char** argv)
    {
      printf("%d\n", validate(argv[1]));
      return 0;
    }
    
    int validate(char ISBN[])
    {
    
      int sum = 0, i = 0;
      int numElements = strlen(ISBN);
      
      if (numElements % 10)
        return 0;
      else
        return 1;
    }

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well it definitely sounds like homework. But what "above caluclation"?

    Why not run a loop over each character, check if isdigit() or it's an X and do it that way, because a space and dashes aren't digits.

    eg:
    Code:
    #include <ctype.h>
    /* ... */
    for(i = 0; i < len; i++)
    {
        /* it's a digit */
        if(isdigit(whatever[i]) != 0)
        {
            /* do whatever with the digit */
        }else if(whatever[i] == 'X') {
            /* it's an X... which means... :o! */
        }
    }
    Last edited by zacs7; 09-15-2007 at 08:47 PM. Reason: Spelling

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    It is homework that I am working on and that's why I've come here to ask for help with it.

    So something like

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int validate(char ISBN[]);
    
    int main(int argc, char** argv)
    {
      printf("&#37;d\n", validate(argv[1]));
      return 0;
    }
    
    int validate(char ISBN[])
    {
    
      int sum = 0, i = 0;
      int numElements = strlen(ISBN);
      
      for(i=0; i < len; i++)
      {
        if(isdigit(ISBN[i]) !=0)
        {
         /*...*/
        }
       else if(ISBN[i] == 'X'
       {
         'X' = 10
       }
    
      if (numElements % 10)
        return 0;
      else
        return 1;
    }
    Is that right?

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    Sorry, but also if X can only be the last 'digit' entered is there anyway to factor that in as well?

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > 'X' = 10
    Isn't right, that's like saying, 88 = 10. 'X' is constant, so is 10.

    Your homework doesn't say that you need to validate it, it also says that there may be 10 or more digits...
    It looks like you've just copy and pasted by example without reading, or understanding it...

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    I am trying to understand it I just can't get my head around it sorry. I've never done any programming before this subject so it's a little tricky for me sorry.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Jailan View Post
    It is homework that I am working on and that's why I've come here to ask for help with it.
    Think about this statement a few times.

    You're paying money to be taught something that you have to come here and ask total strangers for help with. It's not that we don't wish to help, but if you need to get our help because the course help is inadequate, why are you taking the course?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but also if X can only be the last 'digit' entered is there anyway to factor that in as well?
    Of course there is, use && to compare the index to see if you're at the last char
    if ( i == len && char == 'X' )
    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.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    >if ( i == len && char == 'X' )
    Would it not be, if(i == len - 1 ... ? Since the condition in the loop is i < len, ie the loop will only run if i is less than len

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I've no idea, it was merely a description of the possible, to make the OP think.
    It wasn't exactly a "do this and it will work" post.
    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. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 05:13 PM