0

Please help me, i have expected expression error for else if and else, but I don't understand why. Thank you. here is my code

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

int main(int argc, string argv[])
{
     //select the second string which is the key
    string key = argv[1];
     //only 1 string is allowed next to the command
    if (argc != 2)
    {
        printf("error\n");
        return 1;
    }

     //convert the string key to an intefer 
    int k = atoi(key);
    if (k > 0)
    { 
        printf("plaintext: ");
    }
    string plntxt = get_string();
    if (plntxt != NULL)
    {
        printf("ciphertext: ");
    }

    int c = 0;
    int p = 0;

    for (int i = 0, n = strlen(plntxt); i < n; i++)
    {
        if (isalpha(plntxt[i]));
        {
            if (islower(plntxt[i]));
            {
                p = plntxt[i] - 97;
                c = (p + k)% 26;
                int letter = c + 97;
                printf("%c", (char)letter);
            }

            else if (isupper(plntxt[i]));
            {
                p = plntxt[i] - 65;
                p = (p + k)% 26;
                int nletter = c + 65;
                printf("%c", (char)nletter);
            }
        }  
        else 
        {
            printf("%c", (char) plntxt[i]));
        }
    }
    printf("\n");
    return 0;
}
2
  • hi supernovacoder :) it s written that you edited 13 min ago but I don t what you edited ? could you please tell me ? because it will be great to get those hints to help me finish for real that homework!! ;)
    – Genevie
    Apr 25, 2017 at 23:25
  • When you simply copy and paste your code into the question's text box, it comes out unindented and hard to read. Select all your code and click {} to make it readable. I simply did this so it would look indented and cleaner. :-) Apr 26, 2017 at 0:13

1 Answer 1

0

When you compile your program, you get this error:

function.c:76:19: error: expected expression
              else
              ^

What does this mean? It means that your if/else statement isn't connected. In other words, there's something blocking it. See here for more.

In your code, you have a lot of extra brackets ({}) around your printf statements. This is causing the error. For example, you have:

if (isupper(plntxt[i]));
                 {
                      // convert the asc II to alpha index 
                     p = plntxt[i] - 65;
                      //encrypted
                     c = (p + k) % 26;
                      // convert the encrypt to ascII 
                      int nletter = c + 65;
                     {
                          printf("%c", (char)nletter);
                     }
                 }

There's no need for the brackets around printf("%c", (char)nletter);. This confuses the compiler and "blocks" the original if/else statement. When you remove the brackets (as well as the other brackets surrounding parts of your code), your code works perfectly.

Edit

This is an example of how you can fix your code (used comments to keep in academic honesty):

if(isalpha(plntxt[i]))
{
    if(islower(plntxt[i]))
    {
        // put code line by line (neatly) with no brackets
    }
    else if(isupper(plntxt[i]))
    {
        // put code line by line (neatly) with no brackets
    }
}
else
{
    // print unaltered non-alphabetical letters
}

If this answers your question, please hit the green checkmark!

16
  • Oh !!! thank you so much, I finish the logic on sunday 3pm, and I spent about 15hours for the syntax. I spent more time about syntax than the logic of the problem :( I read the link Note and I remove the brackets, but still have the same error : expected expression. P.S: Please how did you compile to get the error message ? I will edit my question with the new code
    – Genevie
    Apr 25, 2017 at 23:56
  • Does it work when I copy with the {} ? is it more easy to read ?
    – Genevie
    Apr 25, 2017 at 23:58
  • You have to select all your code after pasting it in the text box and then click the {} at the top of the text box. I used make caesar and my terminal told me the error. What are you using to compile your file? Apr 26, 2017 at 0:16
  • I suggest re-typing your code very neatly (you can copy your code, paste it in a new file, and then write it line-by-line). When I retyped your code, I got it to work. If you're still having trouble, I'll show you an excerpt of how I corrected your code so you can apply it to the rest of your code. :-) Apr 26, 2017 at 0:19
  • Oh I finally got it. Well, being all aligned, isn,t working copying ? Well If I right it again line by line, isn't supposed to give the same error, because I actually never been able to add else in this caesar, even at the beginning. Yes please, show me an example that I can finish with this. Please
    – Genevie
    Apr 26, 2017 at 0:28

You must log in to answer this question.

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