I have an assignment where I have to produce a rectangle or square based off of input values.
For example; the user inputs height = 3 and width = 3

***
***
***

But I also need to make a hollowed out box, so it's like this
height = 4 and width = 3

***
* *
* *
***

There is my code, and the hollowed out box.

int height, width;
int i = 0;
int j = 0;


cout << "Input digit for height: ";
cin >> height;
cout << endl;

cout << "Input digit for width: ";
cin >> width;
cout << endl;

for (i = 1; i <= height; i++)
{


for (j = 1; j <= width; j++)
    cout << "*";
    cout << endl;
}

_getch();

cout << endl;

for (i = 1; i <= height; i++)
{
 if ((j == 0) || (j == width - 1)) {

for (j = 1; j <= width; j++)
    cout << "*";
if (j =1 j -2; j++;)
    cout << " ";

    cout << endl;

}

_getch();re
return 0;
}

Any code critiquing is much appreciated.
Thank you all

Proper indentation of lines makes it much easier to read, hard to tell if a piece of code executes inside the for loop or outside. But I can see what you're trying to do.

Should look like this, with proper indentation using spaces and tabs:

for (i = 1; i <= height; i++)
{
    // reset j here to zero
    if ((j == 0) || (j == width - 1)) {
        for (j = 1; j <= width; j++)
            cout << "*";
        if (j =1 j -2; j++;)    // syntax error here
            cout << " ";
    }
    cout << endl;
}

On line 33 you have if (j =1 j -2; j++;) several syntax errors there.

You have the right concept using for loops and checking if j == 0 or j == width - 1

There's quite a few logic errors but you have the right idea. Try working around with it a bit.

Hi, thanks for the reply. I was wondering if you could elaborate a little bit more, I'm still super confused. Everytime I try to edit the function on line 33, it still says I have an error.

Line 33 is a if statement so as an example:

if (condition)
    do something here

int x = 201;
if (x > 0)
    printf("x is greater than 0");
else if (x < 0)
    printf("x is less than 0");
else    // (x == 0)
    printf("x is equal to 0");

But I believe you're trying to use a for loop there, so line 33 should look similar to line 31

for (j = 1; j <= width; j++)
    cout << "*";
for (j = 1; j <= 2; j++)    // logic is wrong, syntax is right though
    cout << " ";

But basically to draw a hollow box you need to understand a few things.

  1. the first and last row should be all asterisks (*)
  2. the first and last column should be all asterisks (*)

So just use 2 for loops, go through each row, then each column in that row.

int i, j;

for (i=0; i<height; i++) {
    for (j=0; j<width; j++) {
        // check for conditions
        // do I draw an asterisk '*'?
        // or do I draw a space ' '?
    }
}

So imagine a 5x4 grid, with rows indexed from 0-4 and columns 0-3

  0 1 2 3
0 * * * * (row 0)
1 *     * (row 1)
2 *     * (row 2)
3 *     * (etc...)
4 * * * *

Go through each row and then each column in that row.

That's all I could get. I'm still not getting it. I feel like it's so easy but I just can't get it. I still don't understand this loop.

for (i=0; i<height; i++) {
        for (j=0; j<width; j++) {
            if ((j == 0) || (j == width - 1))
                for (j = 1; j <= width; j++)
            cout << "*";

            for (j -1; j <= 2; j++)
            cout << " ";}
        cout << endl;
        }

So variable 'i' is the row, while variable 'j' is the column.

Using this example (So imagine a 5x4 grid, with rows indexed from 0-4 and columns 0-3):

  0 1 2 3
0 * * * * (row 0)
1 *     * (row 1)
2 *     * (row 2)
3 *     * (etc...)
4 * * * *

Go through each row and then each column in that row.

You should have something like this:

for (i=0; i<height; i++) {
    // check for first and last row (remember variable 'i' is the column)
    [if statement here] {    // first row = 0, last row = 4
        // time to draw an entire row of asterisks *
        [your code here]
    }
    else {  // all rows in between (rows 1-3 in the example I gave you)
        // go through each column (0-3)
        for (j=0; j<width; j++) {
            // check for first and last column (0 and 3)
            [if statement here]
                // print out a asterisk '*'
            [else]  // every column in between (1-2)
                // print out a space ' '
        }
    }
}

I got it! I did it using just for statements though. It literally was right in front of my face the entire time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.