Re asking this one because the previous answer didn\'t work. Write a C++ program
ID: 3675106 • Letter: R
Question
Re asking this one because the previous answer didn't work.
Write a C++ program that gets two inputs an integer and a character. Use repetition control structures better known as "loops" to output an outline of a triangle shape composed of the character and the width specified by the integer. If the input is an even number, it should be increased to the next odd number. Use meaningful variable names, nested loop statements with proper indentation, appropriate comments, and good prompting messages.
For example, if the integer is 11 and the character is an asterisk (*), the triangle shape would look like this:
Sample screen output 1:
Input Validation: Ensure that input used for the width of the triangle does not exceed the screen display area and do not accept negative values for the variables used.
Enter a value to represent the base of a triangle shape (not to exceed 80): 11 Enter the character to be used to generate the outline of a triangle shape (for eg., #, * $): *
* * * * * * * * * ***********
Do you want to quit the program? (type n for no or q to quit): q
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int base;//for base value
char character;//for character to be printed
char input;//for user input about reprinting
do{
do{
cout<<"Enter a value to represent the base of a triangle shape(<=80) ";
cin>>base;
}while((base < 0) || (base > 80));
cout << "Enter the character to be used to generate the outline of a triangle shape ";
cin>>character;
int z=1;
for (int i=0; i<(base/2); i++)//base/2 is for triangle height
{
for (int j=(base/2); j>i; j--)
{
cout<<" "; // displaying space here
}
cout<<character; // displaying given character here
if (i!=0)
{
for (int k=1; k<=z; k++)
{
cout<<" ";
}
cout<<character;
z+=2; // for space between two lines.
}
cout<<endl; // endl is for new line
}
for (int i=0; i<=base; i++)
{
cout<<character;
}
cout<<" Do you want to quit the program? (type n for no or q to quit): ";
cin>>input;
}while(input!='q');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.