Need help creating hollow triangle!! Please!!

I need to create a hollow triangle using nested loops where the user inputs the base size. I can create half but not the rest it should look for ex if base = 7
---*---
--*-*--
-*---*-
******* this should just be a hollow triangle not all messed up

---*--- I can create this
--*----
-*-----
*******

---*--- or this
----*--
-----*-
*******
but i cant seem to put them together. Here is what I have,the first nested loop creates the first half and the second nested loop creates the second half if i run them seperatly.
Please help me ive been at this for 4 hours just trying to figure it out. but anyhow this what i have.

#include<iostream>
using namespace std;

int main()
{
int rows=1,
num=0,
spaceCounter,
maxRows=0,
base=0;

char symbol;

cout << " Enter a value to represent the base of a triangle shape (not to exceed 80): ";
cin >> base;

while((base<0)||(base>80))
{
cout << " ERROR, number must be between 0 and 80 try again: ";
cin >> base;
}

cout << "Enter the character to be used to generate the outline of a triangle shape (for eg., #, * $): ";
cin >> symbol;

maxRows=base/2;

if((base>=0)||(base<=80))
{

while(rows<=maxRows)
{
spaceCounter=rows;
while(spaceCounter<=maxRows)
{
cout <<"-";
spaceCounter++;
}

cout<<symbol;
rows++;
cout<<endl;
}

rows=1;
while(rows<=maxRows)
{
spaceCounter=0;
while(spaceCounter<maxRows)
{
cout <<"-";
spaceCounter++;
}
cout<<symbol;
maxRows++;
cout<<endl;
if(maxRows==base-1)
break;

}
while(num<base)
{
cout << symbol;
num++;
}
}
}
Last edited on
closed account (48T7M4Gy)
This is a bit of a start on the solution you're looking for.

1. You need to nest the while loop.
2. Hint: I've commented out the input questions, not because they are wrong, it just makes it easier to test what you have done.
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
#include<iostream>
using namespace std;

int main()
{
	int rows = 1,
		num = 0,
		spaceCounter,
		maxRows = 0,
		base = 10;

	char symbol = '*';

	/*
	cout << " Enter a value to represent the base of a triangle shape (not to exceed 80): ";
	cin >> base;

	
	while ((base<0) || (base>80))
	{
		cout << " ERROR, number must be between 0 and 80 try again: ";
		cin >> base;
	}*/

	//cout << "Enter the character to be used to generate the outline of a triangle shape (for eg., #, * $): ";
	//cin >> symbol;

	maxRows = base / 2;

	if ((base >= 0) || (base <= 80))
	{

		while (rows <= maxRows)
		{
			spaceCounter = rows;
			while (spaceCounter <= maxRows)
			{
				cout << "-";
				spaceCounter++;
			}
			cout << symbol;

			int innerCounter = 1;
			while ( innerCounter < rows )
			{
				cout << 'b';
				innerCounter++;
			}

			rows++;

			cout << endl; // do this only at the full end of the line
		}		
	}
}
Last edited on
Topic archived. No new replies allowed.