0

I've just solved my mario.c pset1, everything seems to work, but when I enter a value over 23 like 50 or so, the code is still running and drawing the pyramid then re-prompt the height. I've tried to add an if statement but doesn't work. What's wrong with my code?

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

void repeat(int y, char s) {
        for (int i = 0; i < y; i++, printf("%c", s) );
}

int main(void) {
    int height;
    do {
        printf ("height: ");
        height = GetInt();
    }
    while ( height < 0 || height > 23 );
     int x = 0;
        if (height < 0 || height > 23){
        for (int i = 0; i < height; i++) {
            int total = height + 1;
            int num_spaces = total - x; 
            int num_hashes = 2 + x;
            repeat(num_spaces-2, ' ');
            repeat(num_hashes, '#');
            x++;
            printf("\n");
            }
        }
}

1 Answer 1

0

I ran your code without the if statement and it works fine. It also passed check50. Is it possible that you're still running an executable from an earlier version?

As for the if statement, it has the wrong test condition. The test condition in the while loop correctly looks for invalid input. The new if statement, which isn't necessary, would need to check for valid data.

3
  • Thanks for your comment. I just saw an error in the condition of if statement. I'm not running an earlier version, the file is up to date. It passed check50 that's right, but it should not draw the pyramid if the input is > 23. Did you test that? Commented Jul 5, 2016 at 16:42
  • Yes, I did, and it passed. check 50 also tests for that and passed
    – Cliff B
    Commented Jul 5, 2016 at 17:04
  • I'm sorry for being late, thank you once again. Everything works fine now. Commented Jul 10, 2016 at 15:37

You must log in to answer this question.

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