hollow rectangle

Alright, so I need help with creating a hollow rectangle. I already have a code for a filled out one but I don't know what I need to change to make it hollow. I want to try and make it without strings.

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
#include <iostream>

using namespace std;

void Rectangle(int, int, char);

int main()
{
    int length = 0;
    int width = 0;
    char character = '*';
    cout <<"Alright, I will take a width and length and character from you and turn it into a hollow rectangle!"<<  endl;
    cout <<"What is the length?"<< endl;
    cin >> length;

    {
        cout <<"How wide? ";
        cin >> width;
        cout << "What character? ";
        cin >> character;
        Rectangle(length, width, character);
    }

}

void Rectangle(int lg, int wi, char ch)
{
    int wid = 0;

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

}

Last edited on
Well I would suggest creating code that prints lines allong the edges.
So you would print when
Your current x or y values were at zero OR
Your current x or y values were one less than their respective width (x) or height (y)
1
2
3
4
5
6
7
8
9
for(unsigned int countery=0; countery<height; countery++)
{
    for(unsigned int counterx=0; counterx<width; counterx++)
    { //check if counter x or y is on an edge
       if(counterx==0 || countery==0 || counterx==width-1 || countery==height-1)
            std::cout <<Character;
    }
    std::cout <<"\n";
}
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
38
39
40
41
42
43
#include <iostream>
using namespace std;

void Rectangle(int, int, char);

int main()
{
    int length = 0;
    int width = 0;
    char character = '*';
    cout <<"Alright, I will take a width and length and character from you and turn it into a hollow rectangle!"<<  endl;
    cout <<"What is the length?"<< endl;
    cin >> length;

    {
        cout <<"How wide? ";
        cin >> width;
        cout << "What character? ";
        cin >> character;
        Rectangle(length, width, character);
    }

}

void Rectangle( int lg, int wi, char ch )
{
    int wid;
    int lgt = lg;                      // <==== needs new variable

    while( lgt > 0 )                   // <====
    {
        wid = wi;
        while ( wid > 0 )
        {
            bool edge = ( wid == 1 || wid == wi || lgt == 1 || lgt == lg );   // <==== edge or not
            cout << ( edge ? ch : ' ' );                                      // <==== character or space
            wid--;                                                            // <==== (moved)
        }
        cout << endl;
        lgt--;
    }

}


If I wasn't trying to retain your sample code I might have preferred for() loops here, and renamed counting variables to make them more distinguishable from the rectangle dimensions.
Thanks, that really helped! This seems a lot simpler than what I would've done lol.
Consider using string's fill constructor.
http://www.cplusplus.com/reference/string/string/string/
1
2
3
4
5
6
7
8
9
    const int length{ 10 }, width{ 5 };
    const char print{ '*' };
    
    for( int w{ 0 }; w < width; w++ ) {
        if( w == 0 || w == width - 1 )
            cout << string( length, print ) << '\n';
        else 
            cout << print << string( length - 2, ' ' ) << print << '\n';
    }


**********
*        *
*        *
*        *
**********
Topic archived. No new replies allowed.