error: expected '(' before 'else'

This is a discussion on error: expected '(' before 'else' within the C Programming forums, part of the General Programming Boards category; Hello I am trying to write a program that decrypts a string that the user enters with the caesar cypher. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    error: expected '(' before 'else'

    Hello

    I am trying to write a program that decrypts a string that the user enters with the caesar cypher.

    I am pretty sure the rest of my program is correct but I am getting this error: error: Expected '(' before 'else' for line 37 (if else (plain [i] > 96 && plain[i] < 123)

    here is the program:
    Code:
    /* The program encrypts a string with the caesar cypher*/
    #include<cs50.h>
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<string.h>
    
    int
    main(int argc, char *argv[])
    {
          // check if the argument entered is a digit
          if (!isdigit(*argv[1]) || argc !=2)
          {
                printf("Please enter one number as the argument to use to encrypt\m")
                return 1;
          }
          // convert the argument to an int
          int k = atoi(argv[1]);
          // get the user's plaintext to encrypt
          printf("\nplaintext:   ");
          char *plain = GetString();
          // encrypt
          printf("\cyphertext:  ");
          for (int i = 0, n = strlen(plain); i < n; i++)
          {
                // if the letter is lowercase
                if(plain[i] > 64 && plain[i] < 91)
                {
                      int ascii = plain[i];
                      int p = (ascii - 65);
                      int c = (p + k) % 26;
                      int cypher_p = (c + 65);
                      printf("%c", cypher_p);
                }
                // if the letter is uppercase
                 // this is where the error is
                if else(plain[i] > 96 && plain[i] < 123)
                {
                      int ascii = plain[i];
                      int p = (ascii - 97);
                      int c = (p + k) % 26;
                      int cypher_p = (c + 97);
                      printf("%c", cypher_p);
                }
                // if the element in plain is a space print a space
                if else(plain [i] == " ")
                {
                      printf(" ");
                }
          }
          printf("\n");
    }
    Why do I keep getting that error?

    I literally just started programming like two weeks ago so please explain the fix (unless it is just something really stupid that I'm missing haha).
    Last edited by BigWigly; 03-11-2011 at 03:09 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    You need to change "if else" to "else if".

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
              // if the letter is lowercase
                if(plain[i] > 64 && plain[i] < 91)
                {
                      int ascii = plain[i];
                      int p = (ascii - 65);
                      int c = (p + k) % 26;
                      int cypher_p = (c + 65);
                      printf("%c", cypher_p);
                }
               if else(plain[i] > 96 && plain[i] < 123)   <=== lose the if  
               {
                      int ascii = plain[i];
                      int p = (ascii - 97);
                      int c = (p + k) % 26;
                      int cypher_p = (c + 97);
                      printf("%c", cypher_p);

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Wow I knew it was something stupid

    Thanks haha, I have been tearing my hair out over this.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BigWigly View Post
    Wow I knew it was something stupid

    Thanks haha, I have been tearing my hair out over this.
    Actually the correct grammar there would be ... else if ...

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Actually now I am getting an error for the line:
    else if (plain [i] == " ")
    printf(" ");

    It should be pretty obvious what I want to do.... How do I do it?? haha

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    You can't compare strings with ==, you need to use strcmp. If you want to check a single char, use single quotes: ' '.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    How can I use strcmp() with plain[i] and ' '?

    I'm guessing I would check if it == 0?

    I tried
    Code:
    else if(strcmp(plain[i], ' ') == 0)
    didn't work :'(

    Thanks a lot for the really fast responses by the way

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Sorry, I should have been more clear. It looks like you want to check against a single space character. Single quotes ' ' are for single characters, double quotes " " are for strings. You can compare single characters with ==, so you want if (plain[i] == ' '). If you wanted to compare strings, you would need if (strcmp(some_string, "foobar") == 0). Hope that makes a little more sense.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    YESSSSSSS

    Thank you so much everyone I finally got it to work!

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    You're welcome!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21