My code for mario's steps doesn't seem to be storing the stepHeight variable, and it's driving me crazy. Is it because of scoping? If so, how would I get the value to stick around after the do while loop? Obviously this won't print the pyramid, but I figure it should print hi\n for as many times as the user inputs. As of right now, the program will accept an input, and make sure that it is between 0-23, but will not execute my for loop. Any tips or guidance?

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

        printf("How high are your steps?\t");  
        stepHeight = GetInt();
    }while (stepHeight < 0 || stepHeight > 23);  


    for (int i = 0; i == stepHeight; i++)

Your function main() is declared as type (int). This means that whenever this function is used, we can expect that once it finishes executing, we'll receive some integer from it.

The way main() will give us that integer is by using a line that starts return.... So essentially, when we call main(), as you do by typing in ./mario, or whatever at the terminal, it sets about looking for an int.

When it gets to your line of code that says return(stepHeight);, it thinks that stepHeight is the variable we were looking for, and it exits before running your for-loop.

To fix this, remove the return line from where it is, and add a new line at the very end of main, just before the last }. For this line you want something like this: return 0;.

Why 0? It's arbitrary...we could return anything, because for this program we don't really care so much about what is returned as we do what is printed. But in general, for functions where we care more about what they do than what they return, we use the return value to tell us about how the function did. 0 is commonly used to say, the function operated as expected.

As you get into more complicated programs, it'll be common to put return statements in different places in your function, sometimes causing a function to exit early, if for example, the inputs are unexpected, but for now, it's probably best to just keep the return statement at the very bottom of your main() function.

If you do this and you still don't see your message printing, be sure to take a close look at the condition in your for loop, particularly the condition for continuation:

 for (int i = 0; i == stepHeight; i++)

The for loop only executes if the second term is TRUE. If stepHeight is 16 or something, i == stepHeight will be FALSE. Try to think of a condition to put there that will be TRUE at first and then later, as i gets bigger will change to FALSE.



If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark

  • Hi! Thanks for your quick response. I tried moving the return(); , and my for loop is still not being executed. I can't figure out what's wrong with it, as I think my code looks ok. My for loop should at least execute, but after entering a valid integer, the program finishes. I'm stumped. – Rslag27 Sep 25 '15 at 2:44
  • Hi there, I noticed that too, just after posting the answer. See the edited answer above and check your for loop's continuation condition. – Sam Gerber Sep 25 '15 at 2:49
  • Ugh, I was thinking of it as until i==stepHeight; i++. Thanks for your quick and patient help! – Rslag27 Sep 25 '15 at 3:07

You must log in to answer this question.

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