Printing simple shapes on screen

C

C++ Newbie

The following snippet of code shows how a rectangle of user-defined
height & width may be printed on screen in C++ (the C++ NG appears to
be down to Google Groups).

Referring to my FOR loops, is there any way in which the columns may
be iterated in the outer loop rather than the rows, and still yield a
workable result?

Ask if you want the full source code, I didn't think it was necessary
as this deals more with concepts.

case ('r'): cout << "Rectangle selected\n";
cout << "Enter width\n";
cin >> width;
cout << "Enter height\n";
cin >> height;
columns = rows = 0;

for (rows = 0; rows < height; rows++)
{
cout << '\n';
for (columns = 0; columns < width; columns++)
{
if (rows==0)
{cout << '*';}
if (rows>0 && rows < height-1)
{if (columns == 0 || columns ==
width-1)
{cout << '*';}
if (columns > 0 && columns < width-1)
{cout<<' ';}}
if (rows==height-1)
{cout << '*';}
}
}


break;
 
A

Antoninus Twink

The following snippet of code shows how a rectangle of user-defined
height & width may be printed on screen in C++ (the C++ NG appears to
be down to Google Groups).

No worries - lots of people here know C++, and your problem isn't
language-specific anyway.
Referring to my FOR loops, is there any way in which the columns may
be iterated in the outer loop rather than the rows, and still yield a
workable result?

Yes... Unfortunately, there's no ASCII character to "move up a line"
(this wouldn't even be possible on many early output devices...), but
you can easily use a library to do very sophisticated terminal graphics:
ncurses (or even plain old curses) is a popular example on *nix systems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top