-4

This is the error I am getting:

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

In an else if statement towards the end of the code—"else if(isupper(p_i))"—the error is generated.

I have commented above this 'else if' statement. Please let me know what's wrong. Thank you.

#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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Adi Sivasankaran
  • 518
  • 3
  • 6
  • 20

4 Answers4

6

You have a function prototype between the if and the else:

                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))\

The else block must follow immediately after the if block. If you had put the line int isupper(int p_i); pretty much anywhere else (before the first time you use it), you would not have this error. Even better, you should load this prototype via #include <ctype.h> at the top of your file.

elixenide
  • 44,308
  • 16
  • 74
  • 100
4
                int isupper(int p_i);
//-----------------------------------------------------------
// THE FOLLOWING else if STATEMENT IS THE SOURCE OF THE ERROR
//-----------------------------------------------------------
                else if(isupper(p_i))

Remove this declaration: int isupper(int p_i);.

Use the correct #include directive at the top of your source file to declare isupper function:

#include <ctype.h>
ouah
  • 142,963
  • 15
  • 272
  • 331
3

You cannot use any statement between if and else statement. Example -

if()
{
   //some code
}
//some Code ---> This is wrong //this should not be in between if and else
else
{
   //some code
}
elixenide
  • 44,308
  • 16
  • 74
  • 100
RahulKT
  • 171
  • 4
2

Your function prototypes:

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

do not belong within the main() function (or any function) - though that's technically legal which is why the first is not causing a problem.

However, they cannot live within an 'if / else' construct - you have the second sandwiched between an if clause and an else clause.

The best solution is to put them at the head of the file, or better just use:

#include <ctype.h>

to get the relevant header file with the prototypes in included, then remove the prototypes from the function.

abligh
  • 24,573
  • 4
  • 47
  • 84
  • 2
    While it's odd to put them there, it's perfectly legal. – Angew is no longer proud of SO Feb 21 '14 at 18:47
  • It is not legal to put a function prototype inside an `if` / `then` / `else` construct (unless it's inside a block I suppose); putting inside a function elsewhere is merely odd. – abligh Feb 21 '14 at 18:48
  • While I have not down-voted, I think the distinction is here: "do not belong within the main() function (or any function)", not that moving the prototypes won't fix the problem. – crashmstr Feb 21 '14 at 18:57