0

This is what I have so far:

#include <stdio.h>
int main()
{
    int i, space, rows, k=0;

    printf("Height: ");
    scanf("%d",&rows);


    for(i=1; i<=rows; ++i, k=0)
    {
        for(space=1; space<=rows-i; ++space)
        {
            printf("  ");
        }

        while(k != i)
        {
            printf("# ");
            ++k;
        }

        printf("\n");
    }

    return 0;
}

Whenever I try to insert various kinds of loops so that it will prompt the user until the integer is between 1 and 23, the program either doesn't work or works but ignores the loop. Also, whenever I try to use an expression containing an inequality (for almost any program, not specifically this one), I usually get "expression result unused" or "variable expected" with an '^' pointing at the inequality sign.

I'm obviously not asking for a specific solution here but trying to figure out if it is even possible to insert a loop in(or around) this code. Otherwise I will need to start from scratch. Hints, within reason, are welcome.

Also any general insight into what may be going wrong with my conditions containing inequalities would be appreciated.

edit: One of a few things I've tried:

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

int main(void)
{int i, space, rows, k=0;

    printf("Height: ");
    scanf("%d",&rows);


while(true){
    scanf("%d",&rows);
     (rows<=0 && rows>=23)
    {

        printf("Height must be between 1 and 23: ");
        continue;
    }

    else if (rows>=0 || rows<=23)

    for(i=1; i<=rows; ++i, k=0)
    {
        for(space=1; space<=rows-i; ++space)
        {
            printf("  ");
        }

        while(k != i)
        {
            printf("# ");
            ++k;
        }

        printf("\n");
    }

    return 0;
}
}

This was working as a functional program. When I ran it, though, I got this:

~/workspace/pset1/hello/mario/less/ $ ./loop
Height: 7
./loop
            # 
          # # 
        # # # 
      # # # # 
    # # # # # 
  # # # # # # 
# # # # # # # 
~/workspace/pset1/hello/mario/less/ $ ./loop
Height: 84

2
  # 
# # 
~/workspace/pset1/hello/mario/less/ $ ./loop 
Height: -1

8
              # 
            # # 
          # # # 
        # # # # 
      # # # # # 
    # # # # # # 
  # # # # # # # 
# # # # # # # #          

it wouldn't print a pyramid for anything less than zero or greater than twenty-three but it wouldn't repeat "height" Then I modified only the part of the program where it prompts the user for an integer from "Height must be a number from 0 to 23: " to "Height" and when I ran it again I got three error messages completely unrelated to the part I changed :

loop.c:13:27: error: expected ';' after expression
     (rows<=0 && rows>=23)
                          ^
                          ;
loop.c:20:5: error: expected expression
    else if (rows>=0 || rows<=23)
    ^
loop.c:13:15: error: expression result unused [-Werror,-Wunused-value]
     (rows<=0 && rows>=23)
      ~~~~~~~ ^  ~~~~~~~~
3 errors generated.
make: *** [loop] Error 1      
2
  • Can you please edit your question and add examples of what you are trying to do that isn't working? We could then explain why.
    – Cliff B
    May 19 '18 at 20:32
  • edited original post...when I ran it the first time, it was printing pyramids for any integer between 0 and 23 but it wouldn't loop the phrase inside the bracket where it says "Height must be between 1 and 23:" if I typed an integer less than zero or greater than twenty three May 19 '18 at 23:39
1

The construction of the while loop is flawed. In the example, while (true){ ... } is a forever loop. It will run the code inside the curly brackets without end, unless something inside the loop causes it to terminate. The last line inside the loop, return 0; does exactly that. In fact, it guarantees that it only runs once.

Next, there's this: (rows<=0 && rows>=23) This line looks like it's supposed to be a test condition for the while loop, but it isn't associated with any if or while statement anywhere. The compiler looks at it and rejects it as a line of code that doesn't have a closing semicolon.

There's also an else if that doesn't follow an if statement, so it fails too.

It looks like you're trying too hard to do this. Keep it simple. You want to get a piece of data and check that it's valid. That says that you need a loop that runs at least once, meaning that you need to use a do-while loop, not a while loop. The loop needs a condition that says to rerun the loop if the input is something like (rows<=0 && rows>=23) Well, this is close, but not quite right.

Also, it should NOT include the rest of the program in the loop.

This would be the time to do an in-depth review of class material on do-while loops and while loops, their structure and their differences.

BTW, are you doing the less or more comfortable pyramid? How many hashes are supposed to be on the first line?

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

4
  • when~ loop "do{ printf("hello world" ); } while(i<=0 ); return 0;" " is inserted into the program, it will print "hello world" one time but ignores the condition while(i<=0 ) May 20 '18 at 12:19
  • If the number entered was >0, then it worked as designed. Since there's a return after the do while loop, it will exit at that point. If something else is going on, I would need to see the current code.
    – Cliff B
    May 22 '18 at 5:15
  • project is now functioning as intended. thanks for your help! May 23 '18 at 6:13
  • Great to hear. Could you please accept the answer so that the question doesn't stay in the unanswered question pool forever?
    – Cliff B
    May 23 '18 at 10:19

You must log in to answer this question.

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