using namespace std; void printRectangle) int main) /Ideclare the needed variabl
ID: 3702832 • Letter: U
Question
using namespace std; void printRectangle) int main) /Ideclare the needed variables int theChoice; /call function Menu MakeChoice, which /check if the choice is valid returns the choice do thechoice Henu MakeChoice) function call to get user choice ) while (thechoice 4) -0 //print the shape according to different choice be used structure here //a selection structure should //switch selection is the best return 0; /menu for choosing shape to draw int Menu MakeChoice) int choice; in" cout "t1. Squareltltlt in" coutExplanation / Answer
#include <iostream>
using namespace std;
int Menu_MakeChoice()
{
int choice;
cout<<" ******************************************************************************* ";
cout<<" 1. Square ";
cout<<" 2. Rectangle ";
cout<<" 3. Upward Right Triangle ";
cout<<" 4. Downward Right Triangle ";
cout<<" -1. Exit ";
cout<<" Please enter your choice: ";
cin>>choice;
return choice;
}
void printRectangle()
{
int length,width;
do{
cout<<" Please enter length of rectangle: ";
cin>>length;
cout<<" Please enter width of rectangle: ";
cin>>width;
}while(length<=0 || length>20 || width<=0 || width>20);
cout<<endl;
for(int i=0;i<width;i++)
{
for(int j=0;j<length;j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
}
void printSquare()
{
int length;
do{
cout<<" Please enter the length of the square: ";
cin>>length;
}while(length<=0 || length>20);
cout<<endl;
for(int i=0;i<length;i++)
{
for(int j=0;j<length;j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
}
void printURTriangle()
{
int length;
do{
cout<<" Please enter the length of the Upper Right Triangle: ";
cin>>length;
}while(length<=0 || length>20);
cout<<endl;
for(int i=1;i<=length;i++)
{
for(int j=0;j<length-i;j++)
cout<<" ";
for(int j=length-i;j<length;j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
}
void printDRTriangle()
{
int length;
do{
cout<<" Please enter the length of the Downward Right Triangle: ";
cin>>length;
}while(length<=0 || length>20);
cout<<endl;
for(int i=1;i<=length;i++)
{
for(int j=length-i;j<length;j++)
cout<<"*";
for(int j=0;j<length-i;j++)
cout<<" ";
cout<<endl;
}
cout<<endl;
}
int main() {
// your code goes here
int theChoice;
do{
theChoice=Menu_MakeChoice();
}while(theChoice<-1 || theChoice ==0 || theChoice>4);
switch(theChoice){
case(1):
printSquare();
break;
case(2):
printRectangle();
break;
case(3):
printURTriangle();
break;
case(4):
printDRTriangle();
default:
break;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.