Search code examples
ccs50

Why is my code printing a block of hashes instead of a pyramid?


thanks in advance for the help.

I just started CS50 online and am having issues with the jump from the Scratch pset0 to C pset1. With supplemental instructional videos, trying to make sure I understand what I'm doing going through the process, I'm having an issue where the code below should print a pyramid, however prints a block of hashes instead. Take for example if user inputs a height of 3, instead of 1 hash\n, 2 hash\n, 3 hash I get a print of all three lines having 3 hashes. Could someone enlighten me on what I'm missing or may be overlooking?

Please be kind as I'm still fairly new here and to programming in general. My last question asked didn't receive great responses. Many thanks!

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

int main(void)
{
    int height, row, column;
    do
    {
        height = get_int("Height: ");
    }
    while (height < 1 || height > 8);

    for (row = 0; row < height; row++)
    {
        for (column = 0; column <= row; column++)
        {
            printf("#");
        }
        printf("\n");
    }
}

Screenshot of results when executed

I believe the issue falls within the nested for loop initializing column = 0. I expected this code to print a pyramid of the height given by the user, instead it prints a block of the corresponding height and same amount of hashes on each line.


Solution

  • Per @AndreasWenzel via comments:

    There appears to be something wrong in the command prompt of your screenshot. You should be in the directory mario-less, but you appear to be in the root directory of your workspace (I don't mean the absolute root directory). Therefore, my guess is that you have several versions of mario.c in your CS50 codespace, one in the directory mario-less and one in the root directory of your workspace, and you are confusing both of them. You can type the command pwd (print working directory) in the terminal to see in which directory you currently are.

    Answer accepted! Thank you