0

Here is my code so far:

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

int main(void)
{    
    int x;
    #include <stdio.h>
#include <cs50.h>

int main(void)
{    
    int x;
    printf("Height:");
    do
    {
    x = GetInt();
     printf("Height:");
    }
    while ((x > 23) && (x <= 0));

    int i;

    for (int i = 0; i < x; i++);
    {       printf("\n");
            int s;
            for (int s = 0; s < x - 1 - i; s++);
        {
            printf(" ");
        }
         int h;
         for (int h = 0; s + h < i + 2; h++);
         {
             printf("#");
         }
    }

}

It won't compile for some reason, here are all the error codes:

clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    mario.c  -lcrypt -lcs50 -lm -o mario
mario.c:11:1: error: function definition is not allowed here
{    
^
mario.c:47:14: error: declaration shadows a local variable [-Werror,-Wshadow]
    for (int i = 0; i < x; i++)
             ^
mario.c:45:9: note: previous declaration is here
    int i;
        ^
mario.c:50:22: error: declaration shadows a local variable [-Werror,-Wshadow]
            for (int s = 0; s < x - 1 - i; s++)
                     ^
mario.c:49:17: note: previous declaration is here
            int s;
                ^
mario.c:55:19: error: declaration shadows a local variable [-Werror,-Wshadow]
         for (int h = 0; s + h < i + 2; h++)
                  ^
mario.c:54:14: note: previous declaration is here
         int h;
             ^
mario.c:54:14: error: unused variable 'h' [-Werror,-Wunused-variable]
         int h;
             ^
5 errors generated.

Thanks in advance!

1

Simple. You're declaring i, s and h twice each - once just before each for loop and once in the setup statement for each for loop. You can either remove the keyword int from each for loop statement, or you can completely remove the int i; and similar statements as unnecessary.

FYI, a shadow variable (indicated in some of the messages) occurs when you declare two variables with the same name and the second variable has more local scope. Usually, this occurs when the first declaration occurs before a set of curly braces and the second one occurs inside those braces. The second, or shadow variable will have precedence and scope inside the curly braces, masking the original. When processing moves past the closing curly brace, the shadow var no longer exists and the original reappears.

You will learn more about scope as the course proceeds.

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

0

You have duplicated the first 6 lines in your file - remove these and you should get rid of the first error, since you have an unfinished main function with one opening { but no closing }.

It's also not necessary to declare i, s or h before the for loops, as they are declared in the brackets after for.

You must log in to answer this question.

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