[try Beta version]
Not logged in

 
hollow square

Dec 4, 2011 at 5:02pm
I have been working on this code for an hour to the point I can't even remember what I started with:) It won't print the square on the screen!

Here it is:
#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

void hollowsquare(char *, int size);

int main()
{int size;
cout << "Enter an integer between 1 and 20 to determine the size\n";
cout << "of the side of the square.\n";
cin >> size;
return 0;
}
void hollowsquare(char *, int size)
{int row(size);
{for (int line = 1; row=size; row++)
{
for(int column = 1; column <= size; column++)
{cout << "*";
}
cout << endl;
}
}}

Any ideas?

Dec 4, 2011 at 5:17pm
Your biggest problem is that you never actually call the function. Also, there's an error in your first for loop (assignment instead of condition). Though even then, this is a full square. Other problems: terrible formatting, abuse of {}s, unused char* argument, unused headers.
What your algorithm should do:
1. print '*' size times
2. print a '*', size-2 ' 's and another '*'
3. repeat step 2 size-2 times
4. repeat step 1.
Dec 4, 2011 at 5:25pm
No offense but this code is beyond bad, it's completely broken. Let me explain..
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
// None of the includes are even used other than <iostream>

void hollowsquare(char *, int size);

int main()
{
	int size;
	cout << "Enter an integer between 1 and 20 to determine the size\n"; // What's the point of '\n' here? Unless it actually was intentional
	cout << "of the side of the square.\n";
	cin >> size;
	return 0; // You never even call hollowsquare so obviously it doesn't do anything
}

void hollowsquare(char *, int size) // What is the char * supposed to be? It's never even used
{
	int row(size); // Why not just use size, what's the point of assigning a new variable?
	{ // Why would you put a bracket here? This isn't even a loop
		for (int line = 1; row=size; row++) // Your making row equal to size, this isn't a boolean.. it should be row == size and what's the point of setting line to 1 when it's not even used?
		{
			for(int column = 1; column <= size; column++)
			{
				cout << "*";
			}
			cout << endl;
		}
	}
}


Here is how it should look.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void DrawSquare( int iSize )
{
	for ( int i = 0; i < iSize; i ++ )
	{
		for ( int i = 0; i < iSize; i ++ )
			std::cout << "*";
		std::cout << std::endl;
	}
}

void main()
{
	int iSize;

	std::cout << "Enter an integer between 1 and 20 to determine the size";
	std::cout << "of the side of the square.\n";
	std::cin >> size;

	DrawSquare( iSize );
}


Some of the stuff I commented on isn't really 'wrong' but there are better ways of going about it. For example, it's not really 'wrong' to put a new line at the end of the first output in main() but it's not really necessary either. Maybe on your screen it is necessary though to make it look good, I don't know.
Last edited on Dec 4, 2011 at 5:32pm
Dec 4, 2011 at 6:54pm
That worked but it only printed a square, not a hollow square.

I took that into consideration and this is the final result!

#include<iostream>

using namespace std;

void hollowsquare( int side );

int main()
{
int side(0);

cout << "Enter an integer between 1 and 20 to determine the size\n";
cout << "of the sides of the square.\n\n";
cin >> side;

hollowsquare(side);

return 0;
}

void hollowsquare( int side)

{
for (int row = 1; row <= side; row++)
{
for (int col = 1; col <= side; col++)
{
if (row > 1 && row < side && col > 1 && col < side)
cout << " ";
else
cout << "*";
}
cout << "\n";
}
system("pause");
}

Thanks for the help guys!
Topic archived. No new replies allowed.