-
Hollow square
Okay, here's my hollow square code, but why is it inserting asterisks in odd spots?
Code:
#include <iostream>
using namespace std;
// begins program execution
int main()
{
int side; // holds size of side of square
int i,j; // holds counters; i = row and j = column
cout << "Input the size of a side of a square (1-20): "; // asks user for size of the side
cin >> side;
// for top row
for ( i = 0; i < side; ++i)
{
cout << "*";
}
// for middle rows subtract 2 to adjust for first and last row
for ( i = 0; i < ( side - 2 ); ++i)
{
cout << "*";
// subtract 2 to adjust for first and last columns
for ( j = 0; j < ( side - 2 ); ++j)
{
cout << " ";
}
cout << "*" << endl;
}
// for bottom row
for ( i = 0; i < side; ++i)
{
cout << "*";
}
cout << endl;
return 0;
} // end main function
-
What's the output? It looks good as far as I can see.
-
Oh yes, I ran it, and:
Code:
// for top row
for ( i = 0; i < side; ++i)
{
cout << "*";
}
cout << endl; // need a newline
// for middle rows subtract 2 to adjust for first and last row
for ( i = 0; i < ( side - 2 ); ++i)
{
-
It looks like you need to enter a new line or an endl after the first line of asterixes and before you start the second line.
-