printing out box issue - explanation needed

Hello, first of all I would like to say I know how to solve the issue I am having but I just cant figure out why it is happening. The code is pasted below, and all it simply does is prints out a box using #. However, the top right corner and bottom right corner is missing the # and I do not understand why, as each loop iterates the same amount of times, so surely each line would end at the same time? Could someone please explain why this is not the case? Thanks

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
  #include <iostream>

using namespace std;

int main()
{
	int height = 10, width = 10;

	for (int i = 0; i <= width; i++)
	{
		cout << "#";
	}

	cout << endl;

	for (int i = 0; i <= height; i++)
	{
		for (int j = 0; j <= width; j++)
		{
			if (j == 0 || j == width)
			{
				cout << "#" ;				
			}

			cout << " ";				
		}
		cout << endl;
				
	}

	for (int i = 0; i <= width; i++)
	{
		cout << "#";
	}


}
Your box 'graphics' have to be 2 characters wider than the height and width dimensions you want displayed.
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
#include <iostream>

void PrintRow(int);

int main()
{
   int height { 10 };
   int width  { 10 };

   PrintRow(width);

   for (int row { }; row < height; row++)
   {
      std::cout << '#';

      for (int col { }; col < width; col++)
      {
         std::cout << ' ';
      }

      std::cout << "#\n";
   }

   PrintRow(width);
}

void PrintRow(int width)
{
   for (int row { }; row < width + 2; row++)
   {
      std::cout << "#";
   }
   std::cout << '\n';
}

############
#          #
#          #
#          #
#          #
#          #
#          #
#          #
#          #
#          #
#          #
############
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>

void PrintRow(int);

int main()
{
	const int height {10};
	const int width {10};

	PrintRow(width);

	for (int row { }; row < height; ++row)
		std::cout << '#' << std::setw(width + 2) << std::setfill(' ') << "#\n";

	PrintRow(width);
}

void PrintRow(int width)
{
	std::cout << std::setw(width + 3) << std::setfill('#') << '\n';
}

Last edited on
Hello DonnaPin,

For a learning experience. Reworking your code:
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
  #include <iostream>

using namespace std;

int main()
{
	int height = 10, width = 10;

	for (int i = 0; i < width; i++)
	{
		cout << "#";
	}

	cout << endl;

	for (int i = 1; i < height - 1; i++)
	{
		for (int j = 1; j < width; j++)
		{
			if (j == 1 || j == width - 1)
			{
				cout << "#" ;				
			}

			cout << " ";				
		}

		cout << endl;
	}

	for (int i = 0; i < width; i++)
	{
		cout << "#";
	}
}

The first for loop you use i <= width. Take time to count this line and you will see that the "<=" prints 1 mote "#" than you need. The for loop starts at (0) zero, which is a good starting point for most things as C++ is a zero based language, but the "<=" does 1 more loop than you need.

Also this first for loop counts as the first line of "height" and the last for loop counts as a second line. So when you get to the nested for loops you only need 8 lines to print.

In the nested for loops, the outer loop use for (int i = 1; i < height - 1; i++). The loop iterator starts at 1. This accounts for the first for loop and the first line. The "height - 1" accounts for the last line from the last for loop.

In the inner for loop I changed the if statement.

See what you think and if it makes more sense now.

Both Furry Guy's and seeplus's methods are good, but first it helps to understand what was wrong first before you get into some more advanced.

The output of the program is:

##########
#        # 
#        # 
#        # 
#        # 
#        # 
#        # 
#        # 
#        # 
########## 


Now it is 10 columns X 10 rows.

Andy

Edit:
Last edited on
My previous post was for internal dimensions. If you want external dimensions then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>

void PrintRow(int);

int main()
{
	const int height {10};
	const int width {10};

	PrintRow(width);

	for (int row { }; row < height - 2; ++row)
		std::cout << '#' << std::setw(width) << std::setfill(' ') << "#\n";

	PrintRow(width);
}

void PrintRow(int width)
{
	std::cout << std::setw(width + 1) << std::setfill('#') << '\n';
}


For info on setw() and setfill() (amd other manipulators) see
https://en.cppreference.com/w/cpp/io/manip

I would have thought that using stream manipulators was easier than using nested loops...
Last edited on
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
#include <iostream>

using namespace std;

int main()
{
	int height = 10, width = 12;

	for (int i = 0; i < width; i++)
	{
		cout << "#";
	}
	cout << endl;

	for (int i = 0; i < height - 2; i++)
	{
		cout << '#';
		
		for (int j = 0; j < width - 2; j++)
		{
			cout << " ";				
		}
		cout << '#' << endl;			
	}

	for (int i = 0; i < width; i++)
	{
		cout << "#";
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

string operator * ( int n, string s ) { return n ? s + ( n - 1 ) * s : ""; }

int main()
{
   int H = 10, W = 10;    // EXTERNAL dimensions
   string solid = string( W, '#' ) + '\n', space = '#' + string( W - 2, ' ' ) + "#\n";  
   cout << solid + ( H - 2 ) * space + solid;
}
Last edited on
Thank you for the replies. I was mostly looking for an explanation about why i was having the issue, so thanks to Handy Andy for that. But thanks to everyone else as the different methods of doing this has been interesting to read through, so many different ways to do the same thing.
Topic archived. No new replies allowed.