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

int main(void)
{
    int h = 0;
    int g = h - 1;
    do
    {
        h = get_int("Height: ");
    }
    while (h < 0 || h >= 9);

    for (int x = 0; x < h; x++)
    {
        for (int ind = g; ind > 0 && g > 0; ind--)
        {
            printf(" ");
        }

        for (int y = 0; y <= x; y++)
        {
            printf("#");
        }

        printf("\n");
        g--;
    }
    printf("%i\n", g);
}

That's is all my code above, and I don't get why if I'll try to compile it, thats happens: Fault You can expect always -1, but it's even worse: -h - 1. I solve this by just replacing implementation of the int g after do while loop, then the whole function works properly. But still: why g = -h - 1, whet it goes right after h

1 Answer 1

1

Look at the first section of code:

int h = 0;
int g = h - 1;
do
{
    h = get_int("Height: ");
}
while (h < 0 || h >= 9);

With this code, g will always be start at -1. This code sets h to 0 and then sets g to 0 - 1. Next, the do loop gets a new value for h from the user. This will not change the value of g.

The code needs to set g after the do while loop.

Make those changes and then see if there are any other issues. ;-)

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

2
  • but code actualy changes the value of g. But in total you're right, seems that's a bug. Nonetheless, thank you!
    – REJEN RJ
    Commented Jun 24, 2023 at 16:11
  • Yeah, I missed the g-- at the end. Still, g will always start at -1, which is the problem.
    – Cliff B
    Commented Jun 24, 2023 at 23:11

You must log in to answer this question.

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