Write in C++ Provied a loop with a menu. Repeat until the user selects quit. Pro
ID: 3671214 • Letter: W
Question
Write in C++ Provied a loop with a menu. Repeat until the user selects quit. Prompt the user to select one of the menu items. Test for numbers that are not in the menu, provide an error message, and then continue. The menu shall contain:
After the user selects a pattern, allow them to specify the size. If the size is smaller than 1 or larger than 9, give them an error message and repeat the specification of the size.
Once the user has correctly selected a pattern and a size, print the pattern using the size specified.
Pattern 1 is a square. Example for size 4:
Pattern 2 is a triangle. Example for size 5:
Pattern 3 is a square with a diaginal of numbers in stars. Example for size 6:
Pattern 4 is a square with a reversed diaginal of numbers in stars. Example for size 7:
Test the program once using each of these values in order:
Pattern 9; when that is rejected try:
Pattern 1, size 11; when that is rejected use:
Pattern 1, size 3
Pattern 2, size 4
Pattern 3, size 5
Pattern 4, size 6
Quit
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void Square(int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
cout<<size;
cout<<endl;
}
}
void Triangle(int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<=i;j++)
cout<<size;
cout<<endl;
}
}
void Diagonal(int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=i;j>0;j--)
cout<<"*";
cout<<size;
for(j=0;j<size-(i+1);j++)
cout<<"*";
cout<<endl;
}
}
void Reverse_Diagonal(int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-(i+1);j++)
cout<<"*";
cout<<size;
for(j=i;j>0;j--)
cout<<"*";
cout<<endl;
}
}
void display()
{
int choice,size;
while(true)
{
cout<<" 1. Square pattern 2. Triangle pattern 3. Diagonal pattern 4. Reverse diagonal pattern 5. Quit "<<endl;
cout<<"Enter you choice::";
cin>>choice;
if(!(choice>=1&&choice<=5))
{
cout<<" Incorrect choice!!! Please try again ";
continue;
}else if(choice==5)
break;
cout<<"Enter Size::";
cin>>size;
switch(choice)
{
case 1:
Square(size);
break;
case 2:
Triangle(size);
break;
case 3:
Diagonal(size);
break;
case 4:
Reverse_Diagonal(size);
break;
}
}
}
int main()
{
display();
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.