Hollow Rectangles

closed account (3vX4LyTq)
I have made a program that makes rectangles of a specified height, width, and character. What I want to do is make it "hollow" but I have no clue how to go about that.

Example:
How high? 5
How wide? 5
What character? *

*****
*___*
*___*
*___*
*****
Note: underscores are placeholders for whitespace

Any tips?

Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #include <iostream>

using namespace std;

void makeRectangle(int, int, char);

int main()
{
    int height = 0;
    int width = 0;
    char character = '*';
    cout <<"I make rectangles! Set height as -1 to quit!\n";
    cout <<"How high? ";
    cin >> height;
    if(height != -1)
    {
        cout <<"How wide? ";
        cin >> width;
        cout << "What character? ";
        cin >> character;
        makeRectangle(height, width, character);
    }
    cout << "Done";


}

void makeRectangle(int hi, int wi, char ch)
{
    int awi = 0;

    while(hi > 0)
    {
        awi = wi;
        while(awi > 0)
        {
            awi--;
            cout << ch << " ";
        }
        cout << endl;
        hi--;
    }

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

using namespace std;

void makeRectangle(int, int, char);

int main()
{
    int height = 0;
    int width = 0;
    char character = '*';
    cout <<"I make rectangles! Set height as -1 to quit!\n";
    cout <<"How high? ";
    cin >> height;
    if(height != -1)
    {
        cout <<"How wide? ";
        cin >> width;
        cout << "What character? ";
        cin >> character;
        makeRectangle(height, width, character);
    }
    cout << "Done";


}

void makeRectangle( int hi, int wi, char ch )
{
    string full = string( wi, ch ) + '\n';
    string hollow = ch + string( wi - 2, ' ' ) + ch + '\n';

    cout << full;
    while ( hi-- > 2 ) cout << hollow;
    cout << full;
}
closed account (3vX4LyTq)
Thanks lastchance. It didn't work at first but I guess my compiler didn't like '\n'. So I just changed it to "\n"
Last edited on
Topic archived. No new replies allowed.