0

I'm pretty sure I have the logic of this problem figured out, but I have spent two weeks working every day struggling to get my code to compile so that I can actually test what I've done. I keep getting errors that I know are stupidly simple, but I've tried everything and now I need help. Anyone who could look at my code and explain where I am going wrong in my formatting or whatever it is would be greatly appreciated. I've read through all the recommended reading for week 1 and watched all the lectures and walkthroughs and shorts and this just isn't sticking. Right now this code is giving me:

error: unused variable 'l' and

error: unused variable 'c'

Does this have something to do with the scope of the variables? I thought I declared them in the correct place but now I'm confused...

int main(void)
{

int h = 0, l = 0, c = 0;

do
{
    printf("Please give me an int between 0-23:/n");
    h = GetInt();
}
while (h < 0 || h > 23);

return 0;



for(int l = 0; l < h; l++)
{    

for(int c = 0; c < h; c++)
 {
        printf("%s", " ");
 }

    for(int c = 0; c < h+2; c++)
    {
        printf("#");
    }
    printf("\n");   

return 0;
}}
0

It does indeed have something to do with scope. You have declared both at the top of your program, but then you declare them again in each of the for loops by including the keyword int. Because the second (or third) declaration is within a set of parentheses (or curly braces), you are defining new local variables using the same var name (c and l) that overrides the higher level variable with the same name. Since you don't use the higher level c or l, you are actually getting warnings about unused variables. The compiler parameters include one that says that all warnings are to be treated as errors, so it won't compile.

To fix this, either remove the later int keywords, or remove the declarations of c and l at the top of the program. In this case, it doesn't matter which you do, but the first method is probably slightly more efficient.

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

3
  • Thank you for your response. The program now compiles but when I input a valid number the program just ends and does not continue on to print my pyramid. Do I need to post a new question to solve this? – Dagny Oct 4 '15 at 1:14
  • Updated: I just removed the first return 0 which allows my program to continue to the for loop and print hashes...but it's not making a pyramid. – Dagny Oct 4 '15 at 1:16
  • Same problem. You have a return statement inside the for loop. For starters, reformat your code with the correct indentations. (btw, spend some quality time with style50 to get a feel for how to format code for ease of reading.) After that, you have some logic problems printing your pyramid. Break it down into smaller parts. First, get the code to print the right number of lines. Next, get it to print the right number of hashes (#) without worrying about the spaces. Finally, get the spaces right. – Cliff B Oct 4 '15 at 1:39

You must log in to answer this question.

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