Draw a rectangle

Hi i am trying to draw a frame with asterisks, i dont know what is wrong with my code. I've tried to use this link http://www.cplusplus.com/src/triangle.zip for assistance but i cant open the zip file.

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
#include <iostream>
using namespace std;

void displayFrame(int w, int h)
{
cout << endl;
for (int i = 1; i <= h; i++)
{
for (int j = 1; j <= w; j++)
{
if ((i == 1) && (i == h))
cout << "* ";
else if ((j == 1) && (j == w))
cout << "* ";
else
cout << " ";
}
cout << endl;
}
}
int main()
{
int width, height;

cout << "Please enter the width and height of the rectangle: "<< endl;
cin >> width >> height;
displayFrame(width, height);

return 0;
}
Last edited on
Your if() statement on line 11
 
if( (i == 1) && (i == h) )

can only ever be true if h==1.

Likewise, line 13 can only ever be true if w==1.

There should be an easier way to do this, though, using strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Warning: I didn't try to compile this...
#include <cassert>
#include <string>
#include <iostream>

using namespace std;

void displayFrame( int w, int h ) {
    assert( h >= 2 );   // Height has to be at least 2 for a top and bottom side
    assert( w >= 2 );  // Width has to be at least 2 for a left and right side

    // Draw row of 'w' asterisks for top side
    cout << string( w, '*' ) << endl;

    // Draw sides: asterisk, width-2 spaces, asterisk
    for( int j = 0; j < h - 2; ++j )
        cout << '*' << string( w - 2, ' ' ) << '*' << endl;

    // Draw row of 'w' asterisks for bottom side
    cout << string( w, '*' ) << endl;
}

thank you jsmith.
Thi is perfect.

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

using namespace std;

void displayFrame( int w, int h ) {
    assert( h >= 2 );   // Height has to be at least 2 for a top and bottom side
    assert( w >= 2 );  // Width has to be at least 2 for a left and right side

    // Draw row of 'w' asterisks for top side
    cout << string( w, '*' ) << endl;
    
    // Draw sides: asterisk, width-2 spaces, asterisk
    for( int j = 0; j < h - 2; ++j )
    cout<< '*'<< string(w - 2, ' ')<< '*' << endl;
        
    // Draw row of 'w' asterisks for bottom side
    cout << string( w, '*' ) << endl;
    
}

int main()
{
int width, height;

cout << "What width (greater than 4) frame would you like to draw?";
cin >> width;
cout << "What height (greater than 4) frame would you like to draw? ";
cin >> height;
cout << endl;
displayFrame(width, height);
cout << endl << endl;
return 0;
}
Last edited on
I'm not sure what else to say. The new code you posted is almost identical to your original post and still has the problems in it that I identified in my original response. I also gave you a complete solution to the problem which you should be able to trivially modify to meet your new requirement.

If you can post some new code that shows you've made some progress I can help further.
jsmith thank you again.
The above program works 100% correct. I've tried to come up with another one. It is a regtangle and double sided with #, the one i did is not equall on both sides. Please help

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

using namespace std;

void displayFrame( int w, int h ) {
    assert( h >= 2 );   // Height has to be at least 2 for a top and bottom side
    assert( w >= 4 );  // Width has to be at least 4 for a left and right side

    // Draw row of 'w' hash for top side
    cout << string( w, '#' ) << endl;
    cout << string( w, '#' ) << endl;
    // Draw sides: hash, width-2-2 spaces, asterisk
    for( int j = 0; j < h - 2; ++j )
        cout << '#'<< '#' << string(w - 2-2, ' ')<< '#' << '#' << endl;
        
    // Draw row of 'w' hash for bottom side
    cout << string( w, '#' ) << endl;
    cout << string( w, '#' ) << endl;
}

int main()
{
int width, height;

cout << "What width (greater than 4) frame would you like to draw?";
cin >> width;
cout << "What height (greater than 4) frame would you like to draw? ";
cin >> height;
cout << endl;
displayFrame(width, height);
cout << endl << endl;
return 0;

}

Last edited on
Let's look at your width. When you ask the user what width they want, the rectangle you draw includes the sides in the width (which makes sense, because the top and bottom sides are included in the height).

So having said that, you have to divide the width into three pieces:
* The first two characters of width are for the '##' on the left side.
* The last two characters of width are for the '##' on the right side.
* That leaves width - 2 - 2 spaces in between.

Given that, there is a very simple fix (change 1 line) to the above program.

And I'll give you a hint. You also need to change line 8 to
 
assert( w >= 4 );  // Width has to be at least 4 for a left and right side 


Thats smart jsmith, thank you. Is it possible to draw a circle?
Sure, but circles are more difficult to draw in text mode and off the top of my head I don't recall the formulas needed (requires sin and cos) to compute the (X,Y) coordinates.
Topic archived. No new replies allowed.