Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The user decides on the maximum width of the isosceles triangle. Use a validation loop to ensure their input is between 2 and 7 (inclusive). Use nested loops to make the triangle, which is composed of boxes, appear on the screen. For example, for this one, the width would be 2.
 -
| |
 -
 -   -
| | | |
 -   -


What I have tried:

for (int n = 1; n<= tri_width; n++){
				for (int i = 1; i <= tri_width; i++){
					cout << "-";
					for (int j = 1; j<= tri_width; j++){
						cout << "| |";
						for (int k = 1; k <= tri_width; k++){
							cout << "_";
						}
					}
				}
				cout << endl;
			}
			cout << endl;
Posted
Updated 8-Oct-22 9:26am
v4
Comments
jeron1 6-Oct-22 13:26pm    
What is your question?
merano99 6-Oct-22 13:51pm    
Based on Greg's response, I formatted the text to get a better view of the desired output.
The code shown does not match the desired result and an question ist still missing.

What you have "tried" just prints a row of hyphens. You've been told that you need to use nested loops, so think about how you would do this manually. A number 2<=n<=7 is entered. In the first row, you need to draw a single box, but three lines of output are needed to create a box:
ASCII
 -
| |
 -
The nth row will contain n boxes, and each row requires 3 lines of output. The first and last of those 3 lines will contain hyphens. The middle row will contain vertical bars with gaps.

Based on this, take a shot at writing the code.

EDIT#1: If this is going to be a true isosceles triangle, the first row must be indented so its box is centered. Only the last row won't be indented.

EDIT#2: To clarify, the output for three boxes should probably look like this:
ASCII
     -
    | |
     -
   -   -
  | | | |
   -   - 
 -   -   -
| | | | | |
 -   -   -
or even this in standard ASCII art :)
ASCII
    +-+
    | |
    +-+
  +-+ +-+
  | | | |
  +-+ +-+ 
+-+ +-+ +-+
| | | | | |
+-+ +-+ +-+
 
Share this answer
 
v4
Comments
gkau 7-Oct-22 15:17pm    
the output should be a little different, the triangles need to be on the left, there is no indentation as shown at the top
Greg Utas 7-Oct-22 15:27pm    
Then it's a right-angle triangle, not an isosceles.
The current source code now looks like this:
C++
for (int i = 0; i <= tri_width; i++) {
	cout << "-";
	for (int j = 1; j <= tri_width; j++) {
		cout << "| |";
		for (int k = 1; k <= tri_width; k++) {
			cout << "_";
		}
	}
}
cout << endl;

and produces the following output with a width of 2 :
- | | __ | | __ - | | __ | | __ - | | __ | | __

but it should be:
 -
| |
 -
 -   -
| | | |
 -   -

Obviously, some things are still not right. Line breaks and spaces are missing and there are way too many characters. With width 2 you can see that only 12 visible characters are expected.

Since three lines are required to create a box and several boxes are next to each other, it would probably be advisable to first assemble the three lines into three strings before outputting them and only output them when the line is complete.
 
Share this answer
 
v2
Comments
gkau 7-Oct-22 15:03pm    
is it possible to use a const variable to represent one triangle and i could use that in the loop to display multiple triangles? I'm having trouble displaying just one triangle
merano99 8-Oct-22 13:46pm    
I guess the question should be, if you could store a square as a variable to build a triangle out of it? In C++ you could do something like that with a class. You could then add the instances in a line and then output a complete line. Within the class you could manage the three lines in a std::string or std::vector. See my Solution 3 to implement it.
Here is a suggestion to wrap the boxes in a C++ variable. The main program becomes trivially simple, since all annoying things happen encapsulated in the class.

C++
strbox boxout, newbox;

// Draw everythin on screen
for (int i = 0; i < tri_width; i++) {
   cout << boxout;
   boxout += newbox;
}

To implement the variable strbox you need a class that could look like this:
C++
class strbox {
public:
	strbox();  // default constructor
	strbox& operator+=(strbox& nb);
	friend ostream& operator<<(ostream& os, strbox& dt);
	std::string get_l(unsigned n);
	void clear();
private:
	std::string _l[3];
};

Now only the methods themselves have to be programmed. I present here only the frameworks.

C++
strbox::strbox() {  clear(); }
void strbox::clear() { _l[0] = " - "; _l[1] = "| |"; _l[2] = " - "; }
std::string strbox::get_l(unsigned n) { /* TODO	*/}
// declare addition operator that adds two Box objects and returns final Box object
strbox& strbox::operator+=(strbox& nb) { /* TODO */  return *this; }
ostream& operator<<(ostream& os, strbox& sbo) { /* TODO */ return os; }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900