for loop problem

Please, help! I have this problem to do and I am a little stuck. I believe I have most of the code down, but some of it I'm not quite sure how to do.

The problem is:
Write a code segment, using For loops, that prints a hollow rectangle of stars whose width and height are specified by two values read from the keyboard. The top and bottom of the rectangle is a solid row of stars, each row between the top and bottom consists of a star, then width -2 spaces, and another star.


This is my code so far.
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;

int main ()

{

int i;

int n;
int n2;
int j;

cout << "Enter the height of the rectangle you want." << endl;
cin >> n; 
cout << "Enter the width of the rectangle you want." << endl;
cin >> n2;

for (j= 1; j<= n; j++) //to repeat the number of rows.
{
for (i= 1; i <=n2; i++)
cout << '*';
cout << endl;

}

return 0;

}


So far when I type in the width and height, it does what I want it to do. But I'm not too sure how to make it hollow. Also, to make a -2 space between the asterisks for the heigh, I'm going to have to use setw (2) right? Any help is very much appreciated!
Last edited on
Actually what they mean is a solid row for the top/bottom, then for the middle:

* (width - 2) SPACES *

e.g. if you had a width of 5

*___*
_^ 3 spaces


For the hollowness, try thinking about what you would always do for the rectangle on those lines.
Last edited on
When I type in the width-2 it just subtracts some of the asterisks.

I'm probably doing something so incredibly stupid. I have everything else right except for this one part.
ugh.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int i;
int h;
int w;
int j;

cout << "Enter the height of the rectangle you want." << endl;
cin >> h; 
cout << "Enter the width of the rectangle you want." << endl;
cin >> w;

for (j= 1; j<=h; j++) //to repeat the number of rows.
{
for (i= 1; i <=w; i++)
cout << '*' << "  ";
cout << endl;

}

return 0;

}


Okay, now all of them have the spaces in between them; however, the first row and last row are separated too.
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
#include<iostream>
using namespace std;

void main()
{

int h;
int w;


cout << "Enter the height of the rectangle you want." << endl;
cin >> h; 
cout << "Enter the width of the rectangle you want." << endl;
cin >> w;

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
			for(int k = (w-3); k < (w-2); k++)
				cout << " ";
			cout << '*';
	}
	cout << endl;
}


system("pause");

}

thats as close as i got
Thank you! Hmm....I tried yours and I tweaked the one part of it.

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

void main()
{

int h;
int w;

cout << "Enter the height of the rectangle you want." << endl;
cin >> h; 
cout << "Enter the width of the rectangle you want." << endl;
cin >> w;

for (int i= 1; i<= h; i++)
{
	for (int j= 1; j <= w; j++)
	{
		if(i == 1 || i == h)
			cout << '*';
		else if( j == 0 )//|| j == w)
			cout << '*';
		else
			for(int k = (w-3); k < (w-2); k++)
				cout << " ";
			cout << '*';
	}
	cout << endl;
}

}


Okay. now...it's doing what it's supposed to do ..I think. The top and bottom widths though are more than what I originally typed in, but it's close!
another thing i see is that the else if statement will never get ran because j will never be 0.
Topic archived. No new replies allowed.