There are a few errors in my code, but the one I really don't understand is

`caesar.c:25:42: error: invalid operands to binary expression ('string' (aka 'char *') and 'int')
           int c = ((int)text + argv[1]) % 26`

Here is my code:


int main(int argc, string argv[])
    for(int i = 0, n = strlen(argv[1]); i < n; i++)
        //make sure the key is valid
        if(argc == 2 && isdigit(argv[1]))
            //calculate cyphertext
            int atoi(argv);
            string text = get_string("plaintext: \n");
                text -= 97;
            else if(isupper(text))
                text -=65;
           int c = ((int)text + argv[1]) % 26

        printf("Usage: ./caesar key");

  • You really shouldn't edit your question to fix the problem. This makes the answer not make sense to people who end up having the same question/problem as you did. : ) – Robert S. Pratt Jan 31 at 12:02

You are missing a { immediately after your main declaration and before your for loop. This means the computer sees the declaration and then a bunch of code, but doesn't understand the code belongs to the function because it isn't enclosed in braces.


I'll add another answer, since this is a new question. You really should have started a new question and not edited your old one as these are not related--anyone looking at this (even when I did earlier) would not have noticed that you were asking something completely new.

In your line int c = ((int)text + argv[1]) % 26 you have two errors. First, and most obvious is the semicolon to end it (the compiler will catch this when the next error is fixed). Next, text is a string, which is really a char array. You cannot perform addition with an array and a value, and you cannot assign an array to an int, even if you try to cast it as such. You will either need to I dex I to the array to get the value you want, or you will need to perform a loop through the array to add up each value (if you want a total of what it holds).

You must log in to answer this question.

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