2014-02-19 4 views
0

ISBN이 C에서 유효한지 확인하려고합니다. 정수가 아닌 배열 값을 무시하려고합니다. 예를 들어 무시하거나 - 공백을 넣습니다. 나는 코드의 다음 섹션에 대한 오류를 받고 있어요 :ISBN이 C 언어로 유효한지 확인합니다.

if(s[jj] == '-' || size[jj] == ' ') 

오류 메시지는 다음과 같습니다 :

error: subscripted value is neither array nor pointer

int checkISBN(char s[]) { 
     int result = 0; 
     int theSize = 18; /* Most ISBNs are 10 digits long, with some being 
          * 13. I just chose 18 incase there are a lot of 
          * blank spaces or dashes in the array. */ 
     int n = 1;   /* This comes in handy for determing if the check 
           character is valid. */ 
     int sum = 0;  /* We divide this value by 11 to determine if the 
           check character is valid */ 
     int jj; 

     for (jj = 0; jj sum += s[i] * n; ++n; 
       /* For the below, the formula works like this: 
       * The sum for the ISBN 0-8065-0959-7 is 

        1*0 + 2*8 + 3*0 + 4*6 + 5*5 + 6*0 + 7*9 + 8*5 + 9*9 = 249 

       * The remainder when 249 is divided by 11 is 7, the last 
       * character in the ISBN. The check character is used to 
       * validate an ISBN. 
       * */ 
     ... 
} 

...

int checkCharacter = sum/11; 

if (checkCharacter == s[size-1]) { 
     result = 1; 
     /* I know this part is wrong. 
     * If the array only contains 10 values, then s[17] will be null. 
     * I'll fix this later. 
     * */ 
     return result;; 
     /* Fix this to return a calculated value based on string */ 
} 

답변

1

단순 오타, size 배열이 아닙니다. 다음과 같아야합니다.

if(s[jj] == '-' || s[jj] == ' ') 
        ^^^ 
+0

감사합니다. 이제 코드가 좋다고 생각 되더라도 세그먼트 오류 오류가 발생합니다. 이유를 설명해 주시겠습니까? int checkISBN (char s []) { \t int result = 0; int i; \t int n = 1; \t int sum = 0; { 경우 ('-'S [I] ==) { ++ I (; i)는 S (나 strlen <난 ++ I = 0)에 대한 \t } 합계 + = S [I] * N; ++ n; } int checkCharacter = sum % 11; if (s [strlen (s) -1] == 'X') { [strlen (s) -1] = 10;} if (checkCharacter == s [strlen (s) -1]) { 결과 = 1;} \t 반환 결과; – user3326110

관련 문제