Answer to Question #256698 in C++ for Dion Martins

Question #256698

The user must enter a positive integer between 5 and 15. If this number is valid, the user must also choose between a triangle (T or t) and a square (S or s). Display suitable error messages if necessary; otherwise, the program must use an asterisk to draw a triangle or a square of the chosen size and display. Use a Select Case structure to make the decision. Display appropriate error messages where applicable.


1
Expert's answer
2021-11-01T08:28:13-0400
#include<iostream>
using namespace std;

int main()
{
	int number;
	char letter;
	cout<<"Please, enter a number from 5 to 15:";
	cin>>number;
	if(number<=15&&number>=5)
	{
		cout<<"Please, enter a letter T(or t) or S(or s) "
			<<"to choose triangle or square:";
		cin>>letter;
		if(letter=='T'||letter=='t'||letter=='S'||letter=='s')
		{
			switch(letter)
			{
				case 'T':case 't':
				{
					cout.width(17);
					cout<<"*\n";
					for(int i=1;i<number-1;i++)
					{
						cout.width(16-i);
						cout<<"*";
						cout.width(2*i+1);
				 		cout<<"*\n";
					}
					cout.width(17-number);
					for(int i=1;i<=number;i++)
					{
						cout<<" *";
					}
					break;
				}
				case 'S':case 's':
				{
					cout.width(5);
					for(int i=1;i<=number;i++)
					{		
						cout<<" *";
					}
					cout<<"\n";
					for(int i=1;i<number-1;i++)
					{
						cout.width(5);
						cout<<"*";
						cout.width(number*2-1);
						cout<<"*\n";
					}
					cout.width(5);
					for(int i=1;i<=number;i++)
					{		
						cout<<" *";
					}
					break;							
				}
			}
		}
		else
		{
			cout<<"Error! Incorrect letter\n";
		}
	}
	else
	{
		cout<<"Error! Incorrect number\n";
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS