I'm almost there in solving the pset1 of cs50 mario.c the problem in my code is when i enter a number more than 1 and less than 23 , it goes in infinite loop and prints spaces all the way without stop. I'm quite sure the problem in the condition of my "spaces for loop" but i don't know where exactly the problem is if anyone can provide me tips to overcome this problem , it will be great ! thanks !

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

int main(void)
    int m ;
    int row , hash , space ; 
    // we will need to prompt the user at least once so , do-while loop
        printf("Height: ");
        m = GetInt() ; 
    while ((m < 0) || (m > 23)) ;

    //Drawing the pyramid 
    for (row = 0 ; row < m ; row ++)
        for (space = 0 ; space < (m-1) ; space--)
            printf(" "); 
        for (hash = 0 ; hash < ((m+1)-(m-1)) ; hash++)

if you followed the logic of your code, you'd find that if you entered a number that is > 1 for the height, m - 1 would become at least 1. since m - 1 will always produce the same value and spaces is initialized with a 0 and gets decreased after each iteration of the for loop, the condition spaces < (m - 1) will be always true and therefore, an infinite loop.

another problem is that even if you fix this, your program still won't output the correct pyramid. I'll leave this to you to think about, but as a hint, recall you want the number of spaces to be 1 less and the number of hashes to be 1 more in each row. you have three loops. make sure you correctly understand what each of them represents! a pen and a paper may help!

as a side note, you probably wanna give your variables more descriptive names. it's generally a bad practice to give your variables names like m. you also probably don't wanna make your code difficult to understand by placing all sorts of magic arithmetic expressions (e.g., m - 1 and (m + 1) - (m - 1).

You must log in to answer this question.

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