1
    // ** Below is the code of mario pset1, its taking input of width multiple times and not executing step after input//
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    int input;
    int i;
    int j;
    int k;
    do
    {
        input = get_int("Width of Pyramid Please:\n");//taking input of width of pyramid
    }
    while(input>0 && input<9);

    for(i=1; i<=input; i++)
    {
        for(j=1;j<=i; j++)
        {
            printf(" ");
        }
        for(k=1; k<=i; k++)
        {
            printf("*/n");
        }

    }
}

The program is not moving forward to print the tower and it's taking input multiple times irrespective of whatsoever the input it's even accepting char and floats.

Can anyone explain what's going on here and how to deal with it?

1 Answer 1

1
 while(input>0 && input<9)

This is saying that as long as input is greater than zero AND less than nine you will keep doing the do-while loop (namely keep asking for input).

I think what you meant to say was that if the input is outside of that range, then you want to keep asking for input.

So, repeat the do-while loop if input is less than one OR if it is greater than eight.

1
  • 1
    You could also do while( !(input>0 && input<9) ) and achieve the same result, but Ricardo's method is the most efficient. Just showing an alternative of how to reverse a condition. ;-)
    – Cliff B
    Sep 3, 2021 at 21:54

You must log in to answer this question.

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