2014-02-21 144 views
-4

這是我得到的錯誤預期表達式:錯誤:else if語句可

test.c:110:21: error: expected expression 
        else if(isupper(p_i)) 
        ^
1 error generated. 

在對代碼 - 結束的else if聲明「else if(isupper(p_i))」生成-The錯誤。

我上面評論過這個'else if'語句。請讓我知道什麼是錯的。謝謝。

#include <stdlib.h>   // The library which contains the 'atoi()' function 
#include <stdio.h>   //   
#include <cs50.h>   // typedef char *string; and GetString() 
#include <string.h>   // 

// 'argv[]' is an array of strings. (Fun fact: A string is an array of characters.) 
// 'argc' is the integer variable which stores the number of strings that are in 'argv[]'. 


int main(int argc, string argv[]) 
{ 
    // VARIABLE DECLARATIONS 
    int k;      // Integer variable for the non-negative, encryption key 
    string plaintext;   // Variable to store the information to be encrypted 
    int n;      // Integer variable for the string length 
    string ciphertext = NULL; // Variable to store the encrypted information 

    // This loop analyzes the command-line argument(s): We need exactly one argument (i.e. argc = 2) 
    if (argc > 2) 
    { 
     printf("\n"); 
     printf("Too many arguments. Please try again.\n"); 
     return 1; 
    }  
    else if (argc < 2) 
    { 
     printf("\n");   
     printf("This program requires that you provide an argument. Please try again.\n"); 
     return 1; 
    } 
    else if (argc == 2) 
    { 
     k = atoi(argv[1]); 
     if (k == 0 || k < 0) 
     { 
      printf("\n");    
      printf("Invalid input: encryption key needs to be a non-negative integer.\n"); 
      return 1; 
     } 
    } 

    // Prompt the user for a string input to be encrypted: 
    printf("\n"); 
    printf("Please enter the information to be encrypted:\n"); 

    plaintext = GetString(); 
    n = strlen(plaintext); 
    printf("n = %d \n", n); 

    // We need to implement Caesar's Cipher algorithm: 
    // But first, we select for alphabets only by using the 'isalpha()' function: 
    for (int i = 0; i < n; i++) 
    { 
     int p_i = plaintext[i]; 
     int isalpha(int p_i); 
     if (isalpha(p_i)) 
     { 
      int islower(int p_i); 

      if (islower(p_i)) 
      { 
       printf("Caesar's algorithm for the lower case goes here.\n"); 
      } 

      int isupper(int p_i); 
//----------------------------------------------------------- 
// THE FOLLOWING else if STATEMENT IS THE SOURCE OF THE ERROR 
//----------------------------------------------------------- 
      else if(isupper(p_i)) 
      { 
       printf("Caesar's algorithm for the upper case goes here. \n"); 
      } 
     } 
     else 
     { 
      for (int j = 0; j < n; j++) 
       ciphertext[i] = plaintext[i]; 
     } 
    } 

    // Program terminates 
    return 0; 
} 

回答

4
   int isupper(int p_i); 
//----------------------------------------------------------- 
// THE FOLLOWING else if STATEMENT IS THE SOURCE OF THE ERROR 
//----------------------------------------------------------- 
       else if(isupper(p_i)) 

刪除此聲明:int isupper(int p_i);

使用正確的#include指令在源文件的頂部聲明isupper功能:

#include <ctype.h> 
2

你的函數原型:

int islower(int p_i); 
int isupper(int p_i); 

不要在main()函數(或任何函數內屬於) - 雖然這在技術上是合法的,這就是爲什麼第一個不會導致問題。

但是,它們不能存在於'if/else'結構中 - 第二個夾在if子句和else子句之間。

最好的解決辦法是把它們放在文件的頭部,或更好的只是使用:

#include <ctype.h> 

來獲得相關的頭文件與包括原型,然後從功能刪除原型。

+2

雖然把它們放在那裏很奇怪,但它是完全合法的。 – Angew

+0

把一個函數原型放在'if' /'then' /'else'結構中是不合法的(除非它在塊內部)。在其他地方放置一個功能只是很奇怪。 – abligh

+0

雖然我沒有倒票,但我認爲區別在這裏:「不屬於main()函數(或任何函數)」,不是移動原型不能解決問題。 – crashmstr

6

你有ifelse之間的函數原型:

   if (islower(p_i)) 
       { 
        printf("Caesar's algorithm for the lower case goes here.\n"); 
       } 
       int isupper(int p_i); 
//----------------------------------------------------------- 
// THE FOLLOWING else if STATEMENT IS THE SOURCE OF THE ERROR 
//----------------------------------------------------------- 
       else if(isupper(p_i))\ 

else塊必須在if塊後緊跟。如果你在其他地方(第一次使用它之前)將int isupper(int p_i);放在了其他地方,那麼你就不會有這個錯誤。更好的是,你應該通過文件頂部的#include <ctype.h>加載這個原型。

3

您不能在if和else語句之間使用任何語句。示例 -

if() 
{ 
    //some code 
} 
//some Code ---> This is wrong //this should not be in between if and else 
else 
{ 
    //some code 
}