2014-02-21 141 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 
}