0

I am a beginner in CS50 2018. I was following the 2018 YouTube lecture 2 - Arrays and wrote the codes in CS50 sandbox as the lecture was proceeding. However, I am getting errors even after writing the code verbatim as seen in the lecture even though the program run without any errors in the lecture.

Here is what I wrote.

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

const int COUNT = 3;

void chart(int score);

int main(void)
{
    int scores[COUNT];
    for (int i = 0; i < COUNT; i++);
    {
        scores[i] = get_int("Score %i: ", i + 1);
    }

    for (int i = 0; i < COUNT; i++);
    {
        printf("Score %i: ", i + 1);
        chart(scores[i]);
    }
}


void chart(int score)
{
    for (int i = 0; i < score; i++);
    {
        printf("#");
    }
    printf("\n");
}

Here is the error message.

$ make scores2
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    scores2.c  -lcrypt -lcs50 -lm -o scores2
scores2.c:13:43: error: use of undeclared identifier 'i'
        scores[i] = get_int("Score %i: ", i + 1);
                                          ^
scores2.c:13:16: error: use of undeclared identifier 'i'
        scores[i] = get_int("Score %i: ", i + 1);
               ^
scores2.c:18:30: error: use of undeclared identifier 'i'
        printf("Score %i: ", i + 1);
                             ^
scores2.c:19:22: error: use of undeclared identifier 'i'
        chart(scores[i]);
                     ^
scores2.c:11:36: error: for loop has empty body [-Werror,-Wempty-body]
    for (int i = 0; i < COUNT; i++);
                                   ^
scores2.c:11:36: note: put the semicolon on a separate line to silence this warning
scores2.c:16:36: error: for loop has empty body [-Werror,-Wempty-body]
    for (int i = 0; i < COUNT; i++);
                                   ^
scores2.c:16:36: note: put the semicolon on a separate line to silence this warning
scores2.c:26:36: error: for loop has empty body [-Werror,-Wempty-body]
    for (int i = 0; i < score; i++);
                                   ^
scores2.c:26:36: note: put the semicolon on a separate line to silence this warning
7 errors generated.
<builtin>: recipe for target 'scores2' failed
make: *** [scores2] Error 1

What am I doing wrong?

1

The whole problem lies in the semicolon after the declaration of the for loop:

for (int i = 0; i < COUNT; i++);

The semicolon indicates to the compiler that the for loop ends, before starting, so that the variable i within the brackets is outside the scope of its previous declaration, and the compiler notifies us of it, the solution is to remove all semicolon after the declaration of the for loop.

1
  • Thanks!! Got it. – Simba Dec 22 '19 at 0:36

You must log in to answer this question.

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