-1

I'm currently working on a project where a user is prompted to enter the height, width, and the character they would like to use for their box. I have to create a solid and hollow box using for loops. I have created the solid one with no problems, but when it comes to the hollow one, I run into some issues. Any help is appreciated.

int main()

{
    int height;
    int width;
    int i, j;
    char ch;

    cout << "Please enter your height: ";
    cin >> height;

    cout << "Please enter your width: ";
    cin >> width;

    cout << "Please enter your character: ";
    cin >> ch;

    for (i = 1; i <= height; i++)
    {
        for (j = 1; j <= width; j++)
            cout << ch;
        cout << endl;
    }

    cout << "Press any key to continue to the next shape." << endl;
    _getch();

    for (i = 1; i <= height; i++)
    {
        for (j = 1; j <= width; j++)
        {
            if (i == 1 || i == width -1 || j == 1 || j == height )
                cout << ch; 
            else cout << " ";
        }
        cout << endl;
    }

    system("pause");
    return 0;

}
Justin Farr
  • 183
  • 3
  • 5
  • 16
  • "Some issues" is not a very good problem statement. Please [edit] your question to include a more specific description. – Ajean Feb 25 '16 at 20:47

2 Answers2

1

You can write it in nested for loop:

if( (i==1 || i==height) || (j==1 || j==width))
    cout << ch;
else
    cout << " ";
0

This is the code to write a hollow box. w is the width, h is the height, and c is the character to use.

int w, h;
char c;

int i, j;

/* write the first line */
for (i = 0; i < w; ++i)
    putchar(c);
putchar('\n');

/* write the inner lines */
for (j = 0; j < h - 2; ++j) {
    putchar(c);
    for (i = 0; i < w - 2; ++i)
        putchar(' ');
    putchar(c);
    putchar('\n');
}

/* write the final line */
for (i = 0; i < w; ++i)
    putchar(c);
putchar('\n');
lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75