0

I've been working on my caesar.c and I've built this so far. Unfortunately when I try to compile it I'm greeted with a flood of error messages surrounding the for loop noted below. I cannot figure out what it is that's gone wrong. Please share with me any insights you may have. Thank you!

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

int k = -1;
string s;

int main(int argc, string argv[])
{
    ...
        s = get_string();
        for (int i = 0, int n = strlen(s); i < n; i++) // this is the line giving me problems
        ...
}

[EDIT: Unrelated code removed for Honor Code compliance.]

This is the stack of error messages I receive back:

caesar.c:19:25: error: expected identifier or '('

        for (int i = 0, int n = strlen(s); i < n; i++)

                        ^

caesar.c:19:25: error: expected ';' in 'for' statement specifier

caesar.c:19:25: error: expected expression

caesar.c:19:48: error: use of undeclared identifier 'n'

        for (int i = 0, int n = strlen(s); i < n; i++)

                                               ^

caesar.c:19:49: error: expected ')'

        for (int i = 0, int n = strlen(s); i < n; i++)

                                                ^

caesar.c:19:13: note: to match this '('

        for (int i = 0, int n = strlen(s); i < n; i++)

            ^

caesar.c:19:51: error: use of undeclared identifier 'i'

        for (int i = 0, int n = strlen(s); i < n; i++)

                                                  ^

6 errors generated.

make: *** [caesar] Error 1

1 Answer 1

0

A structural syntax error can often generate multiple errors, much like falling dominoes. In this case, there appears to be a single error - the second use of int. When declaring multiple vars of the same type, only one type declarator should be used, i.e., one use of int. Remove the second one and the 6 errors should disappear.

This is why it is usually best to resolve errors in order.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.

You must log in to answer this question.

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