boametaphysica boametaphysica - 3 months ago 17
C Question

CS 50- Pset 1 Mario Program

the problem set asks us to create a half pyramid using hashes. Here is a link to an image of how it should look-

enter image description here

I get the idea and have written the program until printing the spaces (which I have replaced by "_" just so that I can test the first half of it.

However, when I try to run my program, it doesn't go beyond the do-while loop. In other words, it keeps asking me for the height of the pyramid and does not seem to run the for loop at all. I've tried multiple approaches but this problem seems to persist.

Any help would be appreciated!

Below is my code-

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

int main(void)

{
int height;

do
{
printf("Enter the height of the pyramid: ");
height = GetInt();
}
while (height > 0 || height < 24);

for (int rows = 1; rows <= height, rows++)
{
for (int spaces = height - rows; spaces > 0; spaces--)
{
printf("_");
}
}
return 0;
}


Running this program yields the following output-

Enter the height of the pyramid: 11
Enter the height of the pyramid: 1231
Enter the height of the pyramid: aawfaf
Retry: 12
Enter the height of the pyramid:

Answer

Your do/while loop condition is incorrect - change:

do {
    ...
} while (height > 0 || height < 24); 

to either:

do {
    ...
} while (height <= 0 || height >= 24); 

or:

do {
    ...
} while (!(height > 0 && height < 24)); 

(whichever you consider to be more readable/intuitive).

Comments