Alright, so I need help with creating a hollow rectangle. I already have a code for a filled out one but I don't know what I need to change to make it hollow. I want to try and make it without strings.
Well I would suggest creating code that prints lines allong the edges.
So you would print when
Your current x or y values were at zero OR
Your current x or y values were one less than their respective width (x) or height (y)
1 2 3 4 5 6 7 8 9
for(unsignedint countery=0; countery<height; countery++)
{
for(unsignedint counterx=0; counterx<width; counterx++)
{ //check if counter x or y is on an edge
if(counterx==0 || countery==0 || counterx==width-1 || countery==height-1)
std::cout <<Character;
}
std::cout <<"\n";
}
#include <iostream>
usingnamespace std;
void Rectangle(int, int, char);
int main()
{
int length = 0;
int width = 0;
char character = '*';
cout <<"Alright, I will take a width and length and character from you and turn it into a hollow rectangle!"<< endl;
cout <<"What is the length?"<< endl;
cin >> length;
{
cout <<"How wide? ";
cin >> width;
cout << "What character? ";
cin >> character;
Rectangle(length, width, character);
}
}
void Rectangle( int lg, int wi, char ch )
{
int wid;
int lgt = lg; // <==== needs new variable
while( lgt > 0 ) // <====
{
wid = wi;
while ( wid > 0 )
{
bool edge = ( wid == 1 || wid == wi || lgt == 1 || lgt == lg ); // <==== edge or not
cout << ( edge ? ch : ' ' ); // <==== character or space
wid--; // <==== (moved)
}
cout << endl;
lgt--;
}
}
If I wasn't trying to retain your sample code I might have preferred for() loops here, and renamed counting variables to make them more distinguishable from the rectangle dimensions.