0

I've been working on this for days now, and have reached a point where I'm afriad I've gone further away from the LOGIC of the problem. At the moment, the complier doesn't print out line 16 and yet when I enter a number I do get a pyramid, only the spaces/hashes don't work properly.

// a programme that builds a half-pyramid, according to user's input

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

int main(void) 
{

int height = GetInt();
int lines; 
int spaces;
int hashes;

    do
    { 
        printf("height of pyramid: "); //prompts user input
    }    
    while ((height < 0) || (height > 23)); 

        for(lines = 0; lines <= height; lines++)
        {
            for(spaces = (lines-height); spaces > 0; spaces--)
            {
                printf(" "); 
            }
            for(hashes = 1; hashes <= (lines +1); hashes++)
            {
                printf("#"); 
            }
                printf("\n"); 
        }
         return 0; 
}         
  • As a side note, you can format your code on stack exchange by going to "edit," highlighting the section that is code, and clicking {}. (Or you can add an additional 4 spaces prior to all lines meant to be code.) This will make it easier to read. I applied the formatting by editing your comment, however my reputation is not high enough for it to go through until it is "peer reviewed." – Dr.Queso Feb 11 '16 at 23:40
0

you have an infinite loop. the condition (height < 0) || (height > 23) never becomes false once it's true because the value of height never changes inside the loop.

also the loop that is responsible for printing spaces may never execute since space may be negative initially.

| improve this answer | |
0

Think specifically about where you want GetInt(); to be. Currently, you are only prompting the user once regardless of what the user enters. Think about where you can move it in your code so that your program continues to prompt your user for input if that input does not fall within the pset's specifications.

| improve this answer | |

You must log in to answer this question.

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