943,625 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 989
  • C++ RSS
Feb 14th, 2008
0

outline of square

Expand Post »
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<length;myBase++)
{
cout <<c;
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ckins is offline Offline
25 posts
since Feb 2008
Feb 14th, 2008
0

Re: outline of square

So you want to do something like this?
C++ Syntax (Toggle Plain Text)
  1.  
  2. eeeeeeeeeeeeeeeee
  3. e e
  4. e e
  5. e e
  6. eeeeeeeeeeeeeeeee
but don't know how? Or you want this?
C++ Syntax (Toggle Plain Text)
  1. eeeeeee
  2. e e e
  3. e e e
  4. eeeeeee
but it ends up like this?
C++ Syntax (Toggle Plain Text)
  1. eeeeeee
  2. eee
  3. eee
  4. eeeeeee
Can you elaborate please? I'm not sure what you are trying to do.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Feb 14th, 2008
-1

Re: outline of square

use:
  1. for (int myHeight =1;myHeight <width; myHeight++)
  2. {
  3. for (int myBase=1;myBase<length;myBase++)
  4. {
  5. if(myBase==1 || myBase==length-1 )
  6. printf(c);
  7. else if(myHeight==1 || myHeight==width-1)
  8. printf(c);
  9. else
  10. printf(" ");
  11. }
  12. printf("\n");
  13. }
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Feb 15th, 2008
0

Re: outline of square

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

I get too many spaces
Reputation Points: 10
Solved Threads: 0
Light Poster
ckins is offline Offline
25 posts
since Feb 2008
Feb 15th, 2008
0

Re: outline of square

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:
c++ Syntax (Toggle Plain Text)
  1. 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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Feb 15th, 2008
0

Re: outline of square

It is very big mistake to write
C++ Syntax (Toggle Plain Text)
  1. printf(c);
Correct is
C++ Syntax (Toggle Plain Text)
  1. printf("%c", c);
.
Remember the definition:
C++ Syntax (Toggle Plain Text)
  1. printf(argument, ...);
Reputation Points: 9
Solved Threads: 11
Junior Poster
zhelih is offline Offline
114 posts
since Sep 2007
Feb 27th, 2008
0

Re: outline of square

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
Reputation Points: 10
Solved Threads: 0
Light Poster
ckins is offline Offline
25 posts
since Feb 2008
Feb 27th, 2008
0

Re: outline of square

Click to Expand / Collapse  Quote originally posted by ckins ...
C++ Syntax (Toggle Plain Text)
  1. for (int myHeight =0; myHeight< myHeight; myHeight++)
Look at this line. This loop will never be executed due to the condition you have imposed:
C++ Syntax (Toggle Plain Text)
  1. myHeight < myHeight
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I can't find whats wrong with my Rot13 function?
Next Thread in C++ Forum Timeline: checking network connectivity?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC