Hi all. First time poster and beginner in programming. I'm having problems with my program seen below. It compiles and will run but I have the following 3 problems.
1. In main() in the while loop entering 'q' does not exit.
2. in the isbnValidation function. The size check for minimum length words will work, but the check for "Invalid ISBN character" doesn't.
3. Every ISBN entered returns as valid. The formula is supposed to multiply the first digit by 10, next by 9 etc (skipping all the dashes) and add up to a weighted total which if valid will divide evenly by 11.
I'm really new to this and I'm finding arrays and strings quite challenging. Any help or advise anyone is willing to give I would really appreciate.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ISBNMIN 10
#define ISBNMAX 14
void isbnValidation(char *p_isbn, char *p_orig);
int main(void)
{
char isbn[ISBNMAX];
char orig[ISBNMAX];
printf("Enter an ISBN [q to exit]: ");
while(scanf("%s", isbn) != 'q')
{
strcpy(orig, isbn);
isbnValidation(isbn, orig);
printf("Enter an ISBN [q to exit]: ");
}
return 0;
}
void isbnValidation(char *p_isbn, char *p_orig)
{
int i, digit;
int total = 0;
int weight = 10;
if(strlen(p_isbn) < ISBNMIN)
{
printf("ISBN must be minimum 10 digits\n");
return;
}
for(i = 0; i < strlen(p_isbn-1); i++)
{
if(p_isbn[i] < '0' || p_isbn[i] > '9' || p_isbn[i] != '-' || p_isbn[i] != '\0')
{
if(toupper(p_isbn[i]) != 'X')
{
printf("Invalid ISBN character\n");
return;
}
}
}
for(i = 0; i < strlen(p_isbn-1); i++, weight--)
{
if(p_isbn[i] != '-')
{
if(p_isbn[i] == 'x' || p_isbn[i] == 'X')
digit = 10;
else
digit = p_isbn[i];
total += digit * weight;
}
}
if(total % 11 == 0)
{
printf("ISBN %s is valid.\n", p_orig);
}
else
{
printf("ISBN %s is not valid.\n", p_orig);
}
}