help with loop

Hello everyone.
I am currently writing a program for class.
the criteria of the program is as follows.

1# write a program that reads in the size of a square and then prints a hollow square of that size out of asterisks.

The program should work for all square sizes between 1 to 20. If the user enters a non valid number ask the user to re-enter it.Also the program should allow the user to create as many squares as the would like before ending the program.

I am having a difficult time figuring out how to hollow the square, as well as getting it to loop the whole process over. Any advice would be greatly appreaciated.


#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{


int square_size;
int width = 0;
int height = 0;


cout << "Enter square size: ";
cin >> square_size;

while (square_size >= 21)
{
cout<< "Size must be between 1 and 20, Enter square size: ";
cin >> square_size;
}

cout <<"\n";
cout <<"\n";
cout <<"\n";

for (width = 0; width < square_size; width++)
{
for (height = 0; height < square_size; height++)
cout << char(42);
cout << char(42)<< endl;
}

if (height == square_size || width == square_size)
{
cout <<"\n";
cout<< "would you like to draw another square (y or n): ";



}
cout << "\n";
cout << "\n";


return 0;
}








Hi, basic problem is that you can't simply 'draw' a symmetric square, because lines and rows in console isn't equally long. So you each time get rectangle. Well, following code should do what you need:
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
47
48
49
50
51
52
int main ()
{

int square_size;
char full;

do
{
	cout << "Enter square size: ";
	cin >> square_size;
	while(square_size < 1 || square_size > 21)
	{ 
		cout<< "Size must be between 1 and 20, Enter square size: ";
		cin >> square_size;
	}
	cout << "Do you want full square (y/n)?\n";
	cin >> full;
	cout << "\n\n\n";
	if (full == 'Y') full = 'y';

	int b;
	if (full != 'y')
	{
		for (b = 0; b < square_size; ++b)
				cout.put('*');
		cout << endl;
		for (int a = 2; a < square_size; ++a)
		{ 
			
			cout.put('*');
			for (b = 2; b < square_size; ++b)
					cout.put(' ');
			cout.put('*');
			cout << endl;
		}
		for (b = 0; b < square_size; ++b)
			cout.put('*');
	}
	else
		for (int a = 0; a < square_size; ++a)
		{ 
			for (b = 0; b < square_size; ++b)
				cout.put('*');
			cout.put('\n');
		}

	cout<< "\n\nwould you like to draw another square (y or n): ";
	cin.ignore(100, '\n');
}
while (cin.get() == 'y');
return 0;
}


I've also add a simple 'empty square' function
Topic archived. No new replies allowed.