0

I’ve gotten stuck on the Less Comfortable Mario set: the height input portion works, but the loop does not execute. I really wanted to figure this out on my own, but I’ve been stuck for days! Please help. Thank you! I wondered if there’s something off with “int spaces = height-1”, but I’ve also set it to a specific integer as a test and it hasn’t worked.

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

int height;

int main(void)
{
    {
        do

        {
             height = get_int("Enter desired height: ");
        }
        while (height < 1);
        return height;
    }

    {         
    for  (int rows = 0; rows < height; rows++)
    {
        for (int spaces = height-1; spaces <= 0; spaces--)
        {
            printf(".");
        }
        for  (int hash = 0; hash < height; hash++ )
        {
            printf("#");
        }
       printf("\n");

}
    }
}  
0

First, the "get height" section doesn't work, exactly. The return statement that follows the while loop terminates the program. Second, isn't there also supposed to be a limit on how high the pyramid can be?

I'm going to assume that you haven't been able to get past the return statement, and stop here. There are other problems, but maybe you haven't had a chance to see them or work on them yet. If you are still having problems after working on it, please edit the question to reflect your understanding of the problem, maybe add some sample output, and leave a comment saying you're still having problems so that the updates get noted for everyone watching the question.

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

| improve this answer | |
  • Sorry, but I am not following what about the return statement terminates the program? I am a total beginner and tried for more than a week to resolve this, comparing it to the lecture examples. (I excluded the “less than 23” condition for now, in an attempt to pare it down to eliminate anything that could be going wrong) The return statement appears to work as it should, accepting positive height input, rejecting negative height input. Thank you for your help. – Kat Jun 12 '19 at 18:10
  • A return statement causes an immediate, unconditional termination of the current code and a "return" to the calling code. No further code will be executed! If a return statement is in a function, it causes an immediate return to the calling function or main. If a return statement is executed in main(), then the program terminates and control is returned to the operating system. In other words, the program ends. – Cliff B Jun 12 '19 at 18:33
  • thank you, that helped a lot! I misunderstood it from the lecture. I thought "return" was necessary to ask for the input again, if the first input was not within the set conditions. I did not realize omitting "return" would have that effect anyway. – Kat Jun 13 '19 at 3:23

You must log in to answer this question.

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