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;
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.
#include<iostream>
usingnamespace 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
}
}
}