2014-02-19 41 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; \t 爲(I = 0; I user3326110

相關問題