1

why this code not left aligned? please help, english isn't my 1st language

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

    do
    {
        printf("height: ");
        h = GetInt();
        if((h <= 0)||(h > 23))
        {
            printf("height: ");
            h = GetInt(); 
        }
    } while((h < 1)||(h > 23));

    // make prompt

    int s = h-1;
    for(int i=0; i<h; i++)
    {
        int ht = i+2;
        int counter = 0;
        if (counter > s)
        {
            printf(" ");
            counter ++;
        }

        for (int k=0; k<ht; k++)
        {
            printf ("#");
        }

        printf ("\n");
        s--;
    }
}
0

Take a look at this piece of code

int counter = 0;
if (counter > s)
{
    printf(" ");
    counter ++;
}

counter will always start at 0, so it's probably never greater than s, and that's why your spaces don't get printed. You should also probably use a for loop for the spaces and not an if condition, just like you did for the hashtags #.

1
  • 1
    thank you so much
    – Brenda
    Jul 19 '16 at 10:17
0

Your question is unclear. If you are asking why the code itself can't be left aligned, as yours is in the question, it's simply to make it more readable.

I think that you're really asking why the pyramid in the output can't be left aligned. The reason is that it would be too easy and trivial to do. The purpose of right-aligning the pyramid is to force you to write a fairly complex program that prints different amounts of spaces and hashes based on program inputs.

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

1
  • thank you for the info
    – Brenda
    Jul 19 '16 at 10:18

You must log in to answer this question.

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