0

I am trying to make a pyramid of hashtags somehow which I haven´t figured out yet, and trying my way step by step. (I´m doing the Cs50 course) When I came this far the program is working wohoo so far so good - BUT made an infinite loop. Why? Can you not have both for and while after eachother?

#include <stdio.h>

//Get positive integer from user
int get_pyramidrows_int(void);

int main(void)
    {
        int rows, i, j;
        string hashtag = "#";

//Get positive integer up to 8
        do
        {
            rows = get_pyramidrows_int();
        }
        while ( rows > 8);
        
//Print hashtags as many as rows
        for (i =0; i < rows; i++)
        j = 0;
        while(j < rows)
    {
        printf("#");
        (i++);
    }
        
}    
    

//Getting positive integer from user
int get_pyramidrows_int(void)
{
    int n;
    do
    {
        n = get_int("Write a number 1-8: ");
    }
    while( n < 1 );
    return n;
}

so now I tried this instead

#include <cs50.h>

int main(void)
{
    //Get prompt from user for rows
    int rows, height, width, i;
    
    do
    {
       rows = get_int("How many rows? ");
    }
    while(rows < 1 || rows > 8);
    
    //Print rows of pyramid
   for(height = 0; height < rows; height++)
    {
        for(width = rows; width > height; width--)
        {
            printf("#"); 
        }
        printf("#\n");
    }
    
}
2
  • #include <cs50.h> is included. I just missed it when I copied the code
    – l_b
    Commented Mar 18, 2021 at 22:01
  • What issue are you having with the new loops? They work for me. Commented Mar 26, 2021 at 16:23

2 Answers 2

1

The infinite loop lies here:

while(j < rows)
{
    printf("#");
    (i++);
}

The loop depends on the value of j being updated and eventually not less than rows. Since j is never changed inside the loop, one of two things happens. Either the loop doesn't start, or if it does, it never stops. There has to be something inside the loop that updates the value in j so that the loop will eventually terminate.

However, there remains the issue with the for loop mentioned elsewhere remains. As written, the for loop only sets j=0 repeatedly. If you want to also execute the while loop inside the for loop, you need to add a pair of curly braces to encapsulate the entire code block to be executed.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • This was extremely difficult for me. I have tried hundreds of times and I thought I understand how you mean sort of but probably not since I didn´t mange at all. I changed everything and the closest I got was this. I am not asking for the coded solution but any tips?? Hope I´m not too stupid for this :( I really want to learn and tried watching tutorials of nested loops but still this is the best I managed.
    – l_b
    Commented Mar 26, 2021 at 16:08
  • ```c #include <stdio.h> #include <cs50.h> int main(void) { //Get prompt from user for rows int rows, height, width, i; do { rows = get_int("How many rows? "); } while(rows < 1 || rows > 8); //Print rows of pyramid for(height = 0; height < rows; height++) { for(width = rows; width > height; width--) { printf("#"); } printf("#\n"); } }
    – l_b
    Commented Mar 26, 2021 at 16:10
0

Edit: I saw one issue and addressed it, but forgot to address the question (something I tend to do). As Cliff points out, you need to increment j within the while loop so that it can increase in value and eventually cause your statement of (j < rows) to be false. I indirectly addressed this, but not in a way that would make it clear as I was also following a flaw in your logic with how you structured it--something unrelated to your actual question and the issue you were wanting to address.


Your problem is how you wrote the code:

for (i = 0; i < rows; i++)
j = 0;
while(j < rows)
{
    printf("#");
    i++;
}

Because the while loop is not within the brackets of the for loop, the compiler sees this:

for (i = 0; i < rows; i++)
{
    j = 0;
}
while(j < rows)
{
    printf("#");
    i++;
}

It is important to remember that a loop or conditional statement that does not have any braces/curly brackets following it will only be associated with one immediately following statement by the compiler. So, the compiler is seeing two different loops, where j is being recreated in the while loop and set to it's default value of 0. Because 0 will always be less than the value of rows (unless that is 0), you will have an infinite loop. You can fix this by nesting the while loop in the for loop like this:

for (int i = 0; i < rows; i++)
{
    j = 0;
    while(j < rows)
    {
        printf("#");
        i++;
    }
}

This should give you your desired outcome.

Hope this helps. If it does, feel free to click the check mark next to my answer. But if not, let me know and I will try to help some more.

10
  • Nope. This doesn't fix the infinite loop.
    – Cliff B
    Commented Mar 18, 2021 at 22:24
  • True, I forgot to mention that j needed to be updated within the while loop. Commented Mar 18, 2021 at 22:34
  • Yeah, it surprised me that you missed that the first time around. ;-)
    – Cliff B
    Commented Mar 18, 2021 at 23:10
  • This was extremely difficult for me. I have tried hundreds of times and I thought I understand how you mean sort of but probably not since I didn´t mange at all. I changed everything and the closest I got was this. I am not asking for the coded solution but any tips?? Hope I´m not too stupid for this :( I really want to learn and tried watching tutorials of nested loops but still this is the best I managed.
    – l_b
    Commented Mar 26, 2021 at 16:08
  • I edited and added my new code up in the question. or should I make another question?
    – l_b
    Commented Mar 26, 2021 at 16:16

You must log in to answer this question.

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