boametaphysica
boametaphysica

Reputation: 61

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: 

Upvotes: 0

Views: 3560

Answers (2)

user2014
user2014

Reputation: 1

is this more simple

         for(int i=0;i<8;i++) {

             for(int j=0;j < (8-i); j++) 
             {

                 System.out.print(" ");
             }
            for(int k=0;k<=(i+1);k++)
            {

                System.out.print("#");
            }
            System.out.println();  
        }

Upvotes: -2

Paul R
Paul R

Reputation: 213060

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).

Upvotes: 3

Related Questions