954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

outline of square

I can make a box with a char quite easily where the user enters two values L, and W using a for loop
but I don't know how to make just the outline. I'm told there are 2 if else statements needed and I've nested my for loop. I don't know how to restrict in the if statement so it will loop char going across, then empty spaces followed by char, then last line gets printed, any help would be appreciated eeee
e e
eeee

for (int myHeight =1;myHeight < width; myHeight++)
{
for (int myBase=1;myBase

ckins
Light Poster
25 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

So you want to do something like this?

eeeeeeeeeeeeeeeee
e               e
e               e
e               e
eeeeeeeeeeeeeeeee

but don't know how? Or you want this?

eeeeeee
e  e  e
e  e  e
eeeeeee

but it ends up like this?

eeeeeee
eee
eee
eeeeeee

Can you elaborate please? I'm not sure what you are trying to do.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

use:

for (int myHeight =1;myHeight <width; myHeight++)
	{
		for (int myBase=1;myBase<length;myBase++)
		{
			if(myBase==1 || myBase==length-1 )
				printf(c);
			else if(myHeight==1 || myHeight==width-1)
				printf(c);
			else
				printf(" ");
		}
		printf("\n");
	}
DangerDev
Posting Pro in Training
485 posts since Jan 2008
Reputation Points: 165
Solved Threads: 59
 

vernon
it's the top box, the numbers are off int myBase =1, and myHeight=1

I get too many spaces

ckins
Light Poster
25 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Did you try to incorporate any of DangerDev's code? Some of the lines using "printf" had problems, but when fixed, yielded a box that lined up for me. You could either change the "printf" statements to make them work or simply convert the "printf" statements to "cout" statements.

You say you "get too many spaces". In your original code that you posted I don't see you displaying any spaces at all. I am assuming that in this line:

cout << c;

c represents a character and that c is not a space. Please let us know whether you have gotten this working and, if not, please post some updated code.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

It is very big mistake to write

printf(c);

Correct is

printf("%c", c);


.
Remember the definition:

printf(argument, ...);
zhelih
Junior Poster
114 posts since Sep 2007
Reputation Points: 9
Solved Threads: 11
 

for (int myHeight =0; myHeight< width-2; myHeight++)
{
cout << c;

}

for (int myHeight =0; myHeight< myHeight; myHeight++)
{
for (int myBase =0; myBase< width-2; myBase++)
{
cout << c;

cout << (" ");
}

cout << c;


}

for (int myHeight =0; myHeight< width; myHeight++)
{
cout << c;

cout << endl;
}

return 0;

}
this is what gives me only two sides of the box, I need a whole box

ckins
Light Poster
25 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 
for (int myHeight =0; myHeight< myHeight; myHeight++)


Look at this line. This loop will never be executed due to the condition you have imposed:

myHeight < myHeight
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You