# check digit

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 09-14-2007
Jailan
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?
• 09-15-2007
Salem
OK, so where is your "program as below" ?
• 09-15-2007
Jailan
Sorry I meant "as below" as in what I wanted to make as the check digit.
• 09-15-2007
zacs7
> 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.
• 09-15-2007
Salem
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.
• 09-15-2007
Jailan
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; }```
• 09-15-2007
zacs7
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! */     } }```
• 09-15-2007
Jailan
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?
• 09-15-2007
Jailan
Sorry, but also if X can only be the last 'digit' entered is there anyway to factor that in as well?
• 09-15-2007
zacs7
> '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...
• 09-15-2007
Jailan
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.
• 09-15-2007
MacGyver
Quote:

Originally Posted by Jailan
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?
• 09-16-2007
Salem
> 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' )
• 09-16-2007
zacs7
>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
• 09-16-2007
Salem
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.
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last