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.
#include <iostream>
usingnamespace 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!
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.