1

I am working on Mario and have written the following code. The problem I have is that I have the correct spacing but my hashes wont print correctly.

Just to be clear I think I know why it isnt working and thats due to the h = h - 1; line not working. But why isn't it working?

The relevant code:

for (int i = 0; i < x; i++)  
{  
    int space = x - 1;  
    int h = x;  
    while (space > i)  
    {  
         printf(" ");  
         space--;  
    }  
    for (int p = x; p + 2 > h; p--)  
    {  
    printf("#");  
    }  
    printf("\n");
    h = h - 1;
 }  
1
  • You should add details about what is incorrect about the way the hashes are printing, and preferably add those details to the title, so that people with the same problem can find this Q&A more easily.
    – Air
    Oct 22 '14 at 15:20
2

The variable h has a local scope, its value exists only inside the for loop. Before each next iteration of the loop, its value is destroyed. So everytime the loop executes, its value is equal to the value of x(even if its value didn't destroy, still that would go wrong because h = x; statement will execute at every iteration, so in reality it will never decrease its value by 1 and the iteration will be carried out by its value equal to 1 unless the last statement is encountered, which automatically gets absorbed by writing h = x; at the beginning of the iteration). You should put that statement(int h = x;) before the for loop to make the variable change the value as you wish.

Good Luck.

5
  • Thank you, but when you say destroyed what do you mean? would all values within my for loop be cleared on every iteration?
    – Code
    Oct 22 '14 at 15:58
  • No, only the values of those variables are destroyed which are DECLARED(not defined/initialized) in the loop will destroy their values. Always remember that scope is governed by the place you declare variables and not where you initialize them. In your case, when int h is defined before the loop, then it acquires scope in whole main() function unlike the previous case where it was only in the for loop.
    – sinister
    Oct 22 '14 at 16:48
  • I am getting a bit confused what is the difference between Declaring and Defining/Initialized? Also to clarify are you saying that if I Declare int h in the loop, every time it loops it will destroy the previous value of H. But if I declare it outside the loop it's value will stay constant through the entire code and I can use it anywhere even in a loop and its value will stay throughout the entire program? Again Thank you for helping me its much appreciated.
    – Code
    Oct 23 '14 at 19:10
  • Yes, the value of that variable will exist in the scope it is declared. In the corrected code(here), it will exist in the main() function. Also don't use the word constant, just say the value exists. If you don't know, then you will know soon why, still if you are eager.
    – sinister
    Oct 24 '14 at 3:09
  • Thank you sinister :)
    – Code
    Oct 26 '14 at 19:12

You must log in to answer this question.

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