0

//To clarify, this question is not about the problem set...this was just me being curious while taking notes during the lecture. Professor Malan showed a Mario background with a variety of walls of different shapes, and it got me wondering how one could take multiple user inputs and use them in separate, nested for loops.

Anyone know how to prompt the user for 2 different inputs for how wide and how tall the bricks should be? In the lecture, Professor Malan shows the use of a single input, n, for both the i and the j for loop. What if you didn't want to output a square of blocks, but rather a rectangle or other shape? How could you take multiple user inputs?

Here is what I have tried so far, but I keep getting errors: enter image description here

2
  • try using || or && instead of ',' in your while loop(). or check my edit
    – grape_
    Commented Oct 20, 2022 at 23:53
  • Ahh, that helped, and I also had to eliminate ; after the for statements. Now it works! Thank you! Commented Oct 21, 2022 at 18:24

2 Answers 2

0

Just ask for input twice. That's the simple way. Or, ask for both numbers and figure out a way to parse the input they submit. (That may be something for a later part of the course.)

1
  • Here's what I am trying, although I keep getting error messages: Commented Oct 19, 2022 at 1:17
0

you dont necessarily need two inputs to do what you're asking.

you can do it by simply subtracting from one of your nested loops' iterations. by subtracting 1 from our user input height, we can alter the size of the box's outcome.

consequently, you can also add to this number (height) and change the size that way as well

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

int main(void)
{
    int height, _width, row, column;

    do
    {
    height = get_int("Height: ");
    _width = get_int("Width:  ");
    }
    while ((height > 8 || height < 1) || (_width > 8 || _width < 1));
    {
      for (row = 0; row < height; row++)
      {
          for (column = 0; column < _width; column++)
          {
            printf("#");
          }
          printf("\n");
      }
}
1
  • So that works if I want to control the size of the output, but what if I want the user to decide the height and width? Commented Oct 19, 2022 at 1:16

You must log in to answer this question.

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