0

I keep getting an error on the last else idk what's wrong with that code.

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])

    {
    int key = atoi( argv[1]);
    string text= get_string("text:");
        {
        for (int i = 0; i <strlen(text); i++)
            {
            
            if (isupper (text[i]))
                {
                printf("%i", (((text[i] - 'A') + key) % 26) + 'A');
                }
            else if (islower (text[i]))
                {
                printf("%i", (((text[i]- 'a')+ key) % 26) + 'a');
                }
            
            else
                {
                printf("%i ", text[i]);
                }
            }
        
        printf("\n");
        return 0;
        }
    else 
    {
        printf("Usage: ./Caesar\n");
        return 1;
    }
}

2 Answers 2

1

I’m assuming by last else you mean the one that returns 1. At a glance it looks like that else does not belong to an if statement. Is this all right of your code?

1
  • is correct. There is no if statement that the final else can be associated with. Also, there's an unnecessary pair of curly braces surrounding the entire if/elseif/else complex before the final else (Lines 12 and 33). It looks like that final else block was supposed to handle validating the parameters given when the program is created. The test for the parameter is missing.
    – Cliff B
    Commented Aug 7, 2020 at 17:37
0

Else does not stand alone. You can use "if, if, if" or "if,if, else" or "if, else if, else". you can use as many ifs and "else ifs" but only one else.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .