1

I am currently trying to print a pyramid of hashes for the Mario problem set (less comfortable), and they won't print. Would someone be able to look at my code and pinpoint where I am going wrong? Thank you so much in advance.

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

int main(void)
{
int height;
int hashes;
int space;

do 
{
    int height = get_int("height: ");
}
while (height < 0 || height > 5);
{
    for (int i = 0; i < height; i++)
    {
        int hashes = i;
        for (hashes = (i + 1); hashes >= height; hashes++)
        {
            printf("#");
        }
    }

  }


}
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Eugene Sh. Jul 30 '19 at 15:58
  • 1
    Watch for the **scope** of variables! Can you see where you are using [shadow variables](https://stackoverflow.com/questions/618769/how-can-i-access-a-shadowed-global-variable-in-c)? – Weather Vane Jul 30 '19 at 16:01

2 Answers2

0

Assume you are using CS50 Lab and make to compile. This program will not compile because of shadow variables (as mentioned in the comments by @Weather Vane), and other errors. Therefore, I assume the executable that's running (i.e. ./mario) was the last good compile of an older version of the source that did not print results.

The for loop in this code is an infinite loop. If you correct the compile errors, you will see nothing but #.

Recommend you delete the "old" compiled version (rm mario in the terminal) and then modify the code so 1) it compiles and 2) does not create an infinite loop.

Remember, you declare a variable including the type, as here: int height;. But when you use the variable (as here int height = get_int("height: ");) you do not use the type declaration.

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15
0

try this code, hope it helps

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


int height ;
do
{printf("height: ");
    height = get_int();
}
while(height<0 || height>23);

for(int i=1;i<height+1;i++)
{

    for(int j=0;j<height-i;j++)
    {
        printf("%s"," ");
    }
    for(int l=0;l<1;l++)
    {
        printf("#");
    }
for(int k=0;k<i;k++)
{
    printf("#");


}

printf("\n");


}
}
Sarthak Kumar
  • 304
  • 5
  • 16