Draw box based on user input.

No idea what I'm doing here. I need to ask for the width of the box and if the box should be solid or not (output is 'y' or 'n')

#include <iostream>
#include <cstdlib>
#include <ctime>

void printRepeat(char c, int repeats) {

for (int i = 0; i < repeats; i++) {
cout << c;
}

}

void printSolidRectangle(char c, int width, int height) {

for (int i = 0; i < height; i++) {
printRepeat(c, width);
cout << endl;
}

}

void printHollowRectangle(char c, int width) {
for (int i = 0; i < width; i++) {
printRepeat(c, width);
cout << endl;
}
}


int main() {

int width;
int height;
int output;
output = 'y', 'Y', 'n', 'N';

cout << "Enter Width: " << endl;
cin >> output;
if ((width < 2) || (width > 40)) {
cout << "Input not understood. Enter width: ";
cin >> output;
}
else {
cout << "Should the box be drawn as a solid or not? (character that is either 'y' or 'n')" << endl;
cin >> output; {

if ((output = 'y') || (output = 'Y')) {
printSolidRectangle('*', width, height);
}
else if ((output = 'n') || (output = 'N')) {
printHollowRectangle ('', width);
}
}

cout << "Here is your box." << endl;

cout << endl << "Done." << endl;


system("pause");
return 0;
}
Hello


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



You should compile your code before you post it as there were errors in what you have. This way you can post any error messages thet you do not understand.

You have a good start to your code, but it does need some work.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <cstdlib>
#include <ctime>  // <--- Not used.
#include <limits> // <--- Added.

void printRepeat(char c, int repeats)
{

	for (int i = 0; i < repeats; i++)
	{
		std::cout << c;
	}

}

void printSolidRectangle(char c, int width, int height)
{

	for (int i = 0; i < height; i++)
	{
		printRepeat(c, width);
		std::cout << std::endl;
	}

}

void printHollowRectangle(char c, int width)
{
	for (int i = 0; i < width; i++)
	{
		printRepeat(c, width);
		std::cout << std::endl;
	}
}


int main()
{

	int width{};
	int height{};
	int output;
	output = 'y', 'Y', 'n', 'N';  // <--- Will not work. No type and the use of the comma operator dows not give you what you are expecting.

	std::cout << "Enter Width: " << std::endl;
	std::cin >> width;  // <--- Changed.

	if ((width < 2) || (width > 40))  // <--- width and height used before receiving a value and uninitialized.
	{
		std::cout << "Input not understood. Enter width: ";
		std::cin >> width;  // <--- Changed.
	}
	else
	{
		std::cout << "Should the box be drawn as a solid or not? (character that is either 'y' or 'n')" << std::endl;
		std::cin >> output;
		{

			if ((output = 'y') || (output = 'Y'))
			{
				printSolidRectangle('*', width, height);
			}
			else if ((output = 'n') || (output = 'N'))
			{
				printHollowRectangle(' ', width);  // <--- Changed. A single character needs something between the single quotes.
			}
		}

		std::cout << "Here is your box." << std::endl;

		std::cout << std::endl << "Done." << std::endl;

		system("pause");

	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
	}
}  // <--- Missing. 

Read the comments in the code.

Look over the program. You have asked for a value of "width" to be entered, except you have used the wrong variable, but you have not entered a value for height.

In "main the if/else if statement may work, but it will not work all the time. The if part would work better as a while loop. Then you could repeat the same thing for height. You also need to check for any value for "width" and "height" that is not a number. This will cause "cin" to fail and the program will stop working.

Until the program is fixed it looks like the solid rectangle will work, but not the hollow as there is more involved printing anything that is not the first and last line.

Hope that helps,

Andy
Thank you so much for your help! I really appreciate this. I did originally try to use the while loop, but it only worked for the width part and not the specifying output.

So I tried this and I'm having trouble with the boxes not printing.


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cstdlib>
#include <limits>
using namespace std;

void printRepeat(char c, int repeats) {

	for (int i = 0; i < repeats; i++) {
		std::cout << c;  
	}
}

void printSolidRectangle(char c, int width, int height) {

	for (int i = 0; i < height; i++) {
		printRepeat(c, width);
		std::cout << std::endl;
	}
}

void printHollowRectangle(char c, int width) {
	for (int i = 0; i < width; i++) {
		printRepeat(c, width);
		std::cout << std::endl;
	}
}


int main() {

	double width{};
	double height{};
	int output;
	int i, j;

	std::cout << "Enter Width: " << std::endl;
	std::cin >> width;

	while ((width < 2) || (width > 40)) {
		std::cout << "Input not understood. Enter width: ";
		std::cin >> width;
	}

	std::cout << "Should the box be drawn as a solid or not? (character that is either 'y' or 'n')" << std::endl;
	std::cin >> output;

	output = 'y', 'Y', 'N', 'n';

	for (int i = 0; i < width; i++) {
			if ((output = 'n') || (output = 'N')) {
				printHollowRectangle(' ', width);
				cout << endl;
			}
			else if ((output = 'Y') || (output = 'y')) {
				printSolidRectangle('*', width, height);
				cout << endl;
			}
		}

		std::cout << "Here is your box." << std::endl;

		std::cout << std::endl << "Done." << std::endl;


		system("pause");
		return 0;
	}
}
Hello snew18,

Your while does the job, but it can be written simply as while (height < 2 || height > 40). The extra ()s are not needed, but OK if you leave them.

You will also need another while loop, which I put first:
1
2
3
4
5
6
7
8
9
10
11
12
13
while (!std::cin)  // <--- If something other than a number was entered.
{
	std::cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	std::cout << "Input not understood. Enter width 2 to 40: ";
	std::cin >> width;  // <--- Changed.
}
while (width < 2 || width > 40)
{
	std::cout << "Input not understood. Enter width 2 to 40: ";
	std::cin >> width;  // <--- Changed.
}

The same concept will work for "height".

This is a simple way of presenting two parts. The first is if anything other than a number is entered, formatted input, and second if the value is out of the range.

Line 47 the use of the comma operator will not work the way you are thinking. In this case "output" will end up with the value of 'n'. Actually for what you are not doing here I would remove line 47. By leaving it "output" is set equal to 'n', so inside the for loop you will always enter the if statement and never reach the else statement.

Starting withe the first function "printRepeat" this will produce a nicer looking rectangle: std::cout << c << ' ';.

The "printSolidRectangle" function works, but the "printHollowRectangle" does not.

From what I am remembering printing a hollow rectangle will require nested for loops. The outer to count the rows and the inner to deal with the columns. I have not found the code that I was looking for yet. You can do a search here and should find something that will work.

line 49 the for loop should be based on "height" not "width" because this for loop would need to deal with the number of lines. Although this will cause a problem with the hollow rectangle. I would say that calling the two functions with "height" and "width" and deal with the rows and columns in those functions.

Hope that helps,

Andy
This is completely wrong.
output = 'y', 'Y', 'N', 'n';


1
2
3
			if ((output = 'n') || (output = 'N')) 
...
			else if ((output = 'Y') || (output = 'y')) {

Common error: = is an assignment operator; == should be used for logical comparison of equality.

Your last code doesn't compile ... because the brackets don't balance ... which is because the indentation is inconsistent.

Your hollow rectangle is just going to print a blank rectangle.
Hello snew18,

I found myself in error earlier when I was thinking the hollow rectangle would need nested for loops. When I started working on the function I found that the inner for loop was handled by the "printRepeat" function and I came up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void printHollowRectangle(char c, int width, int height)
{
	for (int row = 0; row < height; row++)
	{
		if (row == 0 || row == height - 1)
		{
			printRepeat(c, width);
			std::cout << std::endl;
		}
		else
		{
			std::cout << '*';
			printRepeat(' ', width - 2);
			std::cout << " *" << std::endl;
		}

		//printRepeat(c, width);
		//std::cout << std::endl;
	}

}

BTW I was working with your first code not the updated code.

The other function ended up like this:
1
2
3
4
5
6
7
8
9
10
void printSolidRectangle(char c, int width, int height)
{

	for (int i = 0; i < height; i++)
	{
		printRepeat(c, width);
		std::cout << std::endl;
	}

}


Hope that helps,

Andy
Topic archived. No new replies allowed.