making a hollow square

I need to know how to make a hollow box with spaces in between so the code outputs like this for an inputted character. The solid box is fine, I'm just not sure how to create the spaces.

99999999
99999999
99999999
99999999

99999999
9 9
9 9
99999999

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
46
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int main () 
{

int height;
int width;
int count;
int hcount;
string character;

cout << "input width" << endl;
cin >> width;
cout << "input height" << endl;
cin >> height;
cout << "input character" << endl;
cin >> character;

for (hcount = 0; hcount < height; hcount++)
	{
	for (count = 0; count < width; count++) 
	cout << character;

cout << endl;
	}


cout << endl;


for (hcount = 0; hcount < height; hcount++)
	{
	for (count = 0; count < width; count++) 
	cout << character;

cout << endl;
	}

	
_getch();
return(0);
}

Last edited on
Or a link on how to do it, I still am not sure how to increment/change the second loop to make spaces in the box where theyre supposed to be.
Use if or if else statements. Look at it one step at a time:

1) You need to modify the contents after the first line and before the last line of the box. In other words, hcount > 0 and hcount < height - 1.

2) While modifying and you're on the line, after count == 0, there is a space in count == 1. Then on count == 2, the char appears again, then beyond that to whatever width is it's all spaces.

Logic operators will help in solving this ie. && (AND logic operator) and || (OR logic operator).
Got it, thanks
@applesnstuff (4)>

#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int main ()
{

int height;
int width;
int count;
int hcount;
string character;

cout << "input width" << endl;
cin >> width;
cout << "input height" << endl;
cin >> height;
cout << "input character" << endl;
cin >> character;

for (hcount = 0; hcount < height; hcount++)
{
for (count = 0; count < width; count++)
cout << character;

cout << endl;
}


cout << endl;


for (hcount = 0; hcount < height; hcount++)
{
if(hcount==0 || hcount==height-1)
for(count = 0; count < width; count++)
cout<< character;

else
for (count = 0; count < width/4; count++){
cout << character;
cout<< " ";
}

cout << endl;
}


_getch();
return(0);
}
Last edited on
Topic archived. No new replies allowed.