im new to c++ and having trouble completing this program. Write a program that r
ID: 3634295 • Letter: I
Question
im new to c++ and having trouble completing this program.Write a program that reads in the size of a triangle and then prints a hollow triangle of that size out of asterisks and blanks. Your program should work for triangles of all sizes between 1 and 20. When the user enters the triangle size, make sure it is a number from 1 to 20. If it is not a valid number, ask the user to re-enter the number. Allow the user to draw as many triangles as they would like before ending the program. Note: due to the character size, the triangle will not actually look like a right triangle.
Example Output:
Enter the triangle size: 25
Size must be between 1 and 20, Enter the triangle size: 5
*
**
* *
* *
*****
Explanation / Answer
Note: Enter wrong triangle size twice to end program.
#include <iostream>
using namespace std;
int main()
{
//PROTOTYPES
void printRightHollowTriangle(int length);
//LOCAL DECLARATIONS
int length;
//PROCEDURES
do
{
cout << "Enter the triangle size: ";
cin >> length;
cin.ignore();
if (length <= 20 && length >= 1)
{
printRightHollowTriangle(length);
cout << endl;
}
else
{
cout << "Size must be between 1 and 20,"
<< " re-enter the triangle size: ";
cin >> length;
cin.ignore();
if (length <= 20 && length >= 1)
{
printRightHollowTriangle(length);
cout << endl;
}
else
{
cout << "End - Program aborted ";
}
}
}
while (length <= 20 && length >= 1);
cin.get();
return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITION
//---------------------------------------------------------
void printRightHollowTriangle(int length)
{
for (int line = 1; line < length; ++line)
{
cout << "*";
for (int blank = 0; blank < line - 2; blank++)
{
cout << " ";
}
if (line > 1)
{
cout << "*";
}
cout << endl;
}
for (int line = 0; line < length; ++line)
{
cout << "*";
}
cout << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.