1

I recently just tipped my toes into the water of CS50 last week, I'm completely new to this so I need a bit of constructive critisism. I was completed stumped at first, but I got somewhat close - not sure if it is the proper way but that's why I came to you guys. (also i'm not sure why i'm getting the two extra periods each line.) Thanks!

#include <cs50.h>
#include <stdio.h>
//declare integers
int row, space,height,hash; 
int main(void)
{
    //get integer
    do 
    {
        height = get_int("Height: ");
    }
    while (height <= 0 || height > 8);
    if (height > 0 || height <= 8)
        //row
        for (row = 0; row < height; row++)
        {
            //create spaces
            for (space = 0; space <= height - row; space++)
            {
                printf(".");
            }
            //create hash
            for (hash = 0; hash <= row; hash++)
            {
                printf("#");
            }
            //new line
            for (int line = 0; line <= 0; line++)
            {
                printf("\n");
            }
        } 
}   
1

for (space = 0; space <= height - row; space++) runs height - row + 1 times (+1 for having <= instead of <). For the first row, you should have height - 1 spaces, but you have height + 1. Fix that by having for example space < height - row - 1.

Unrelated:

height > 0 || height <= 8 makes no sense, is always true.

I would not declare variables globally. Declare them in a scope as narrow as possible. For example, declare height just in front of your do..while loop (you can't declare it inside, as it has to persist, but you don't need it outside of main). Declare your loop variables in the loop head, like for (int counter = 0; counter < repetitions; counter++).

\n does not need another loop. You don't want multiple per row, so simply printf("\n"); or equivalent, inside the outer loop, after the two inner loops.

1
  • Thanks a lot for the input, I’ll try your advice!
    – Dohsure
    Oct 4 '19 at 15:49

You must log in to answer this question.

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