Need guidance on how to make a rectangle be printed with two characters

Hi, I have this one assignment I am working on and I am a bit stuck here

I have to make a rectangle do this with user input:

Side size = 1
+-+
| |
| |
| |
| |
| |
| |
+-+

----------------------------------------------------------------------------

Here's what I have so far

//Draw a single line of fiver uppercase Xs using two kinds of count-controlled loops
#include <iostream>
using namespace std;

int main()
{

//Variables declared
int count1 = 1, limit = 5, count2 = 1;
char doAgain;


cout << "Enter the width for a line of Xs to be drawn on the screen: ";
cin >> limit;

count1 = 1;
cout<< '+';
while(count1 <= limit-2)
{
cout << "-";
count1++; //Loop update condition
}
cout<< '+';

cout << endl << endl;

while(count1 <= limit-2)
{
cout << " ";
count1++; //Loop update condition
}
cout<< '|';

cout << endl << endl;

while(count2 <= limit-2)
{
cout << " ";
count2++; //Loop update condition
}
cout<< '|';

cout << endl << endl;

return 0;
}

Any guidance would be useful thank you in advance :)
Last edited on
you endl twice which seems wrong after first loop.
just break it down.
you want +
then N dashes, loop it
then + again
then endl

in a loop
{
then | pipe
then N spaces, looped
then pipe again
then endl
} for all the rows

repeat the first line logic here

done.

you do not have this. you have the doubled endl and no | printed after it and no double loop for the rows (one loop down, one loop across each one as I described).
Last edited on
Yeah, I understood what you're saying now the only thing I am having trouble with is putting the pipe on the other side of the rectangle because the column only goes down on one side and does not appear on the other side of the connecting end.





//Draw a single line of fiver uppercase Xs using two kinds of count-controlled loops
#include <iostream>
using namespace std;

int main()
{

//Variables declared
int count1 = 1, limit = 5, count2 = 1;
char doAgain;


cout << "Side Size: ";
cin >> limit;

count1 = 1;
cout<< '+';
while(count1 <= limit-2)
{
cout << "-";
count1++; //Loop update condition
}
cout<< '+';

cout<< endl;

for(int i = 0; i <= count1; i++)
{
cout<< '|' << endl;
}
while(count1 <= limit-2)
{
cout << " ";
count1++; //Loop update condition
}
cout << '|';

cout<< endl;

count1 = 1;
cout<< '+';

while(count1 <= limit-2)
{
cout << "-";
count1++; //Loop update condition
}
cout<< '+';

cout<< endl;
return 0;
}
you have multiple problems still.
you do not set count1 to zero, so you do not actually write any spaces. to see this, change the space to some other char like * until you get it right.
you did not do a nested loop, so once you set count1 back to zero you only get 1 row that is right. Nesting the loops fixes this but then your end of lines need to be cleaned up.

putting all that together gives something like this.

**note the code tags here. please use them going forward.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


//Draw a single line of fiver uppercase Xs using two kinds of count-controlled loops
#include <iostream>
using namespace std;

int main()
{

//Variables declared
int count1 = 1, limit = 5, count2 = 1;
char doAgain;


cout << "Side Size: ";
cin >> limit;

count1 = 1;
cout<< '+';
while(count1 <= limit-2)
{
cout << "-";
count1++; //Loop update condition
}
cout<< '+';

cout<< endl;

for(int i = 0; i <= count1; i++)
{
cout<< '|';
count1 = 0;
while(count1 <= limit-2)
{
cout << "*";
count1++; //Loop update condition
}
cout << '|'<< endl;

}

count1 = 1;
cout<< '+';

while(count1 <= limit-2)
{
cout << "-";
count1++; //Loop update condition
}
cout<< '+';

cout<< endl;
return 0;
}
Thank you so much I understood where I went wrong :)
Topic archived. No new replies allowed.