0

My code won't compile and I can't tell why. All of the error messages I'm receiving are regarding my "for" statement and my use of the variable i. I also get an error message about my use of "string" and the was I used "else if." Could someone point out my error for me?

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

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar k\n");
        return 1;
    }

    int k = atoi(argv[1]);
    string s = get_string;
    printf("plaintext:");

    printf("ciphertext:");
    for (int i = 0, length = strlen(s), i < length, i++)
    {
        char character = s[i];
        if (isupper(character));
        {
            char cipherupper = (((character + k) % 26) + 65); 
            printf("%c", cipherupper);
         }
        else if(islower(character)):
        {
            char cipherlower = (((character + k) % 26) + 97);
            printf("%c", cipherlower);
        }
        else
        {
            printf("%c", character);
        }
    }
        printf("\n");    
        return 0;
}   
0

Put printf("plaintext: "); before your call to get_string. Speaking of which, you have to call it, by using get_string() with parentheses.

Also, you might want to subtract 65 or 97 from your character first, before applying the %26.

3
  • I also figured out that I had commas instead of semi colons in my for loop however I still get an error for my "else if" statement saying "expected expression"? May 8 '17 at 16:57
  • The colon : is rarely used in C (I remember it only in the case statement and as part of the ternary conditional operator condition ? value1 : value2).
    – Blauelf
    May 9 '17 at 8:22
  • Oh, I did not see that you wrote a semicolon after your if's condition. That semicolon is the empty statement, and this, not the code block after it, is executed conditionally. In this environment, the else could not find its if. Remove that semicolon after if (isupper(character)).
    – Blauelf
    May 9 '17 at 8:25

You must log in to answer this question.

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