Provide 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:
1. Square pattern
2. Triangle pattern
3. Diagonal pattern
4. Reverse diagonal pattern
5. Quit
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:
4444
4444
4444
4444
Pattern 2 is a triangle. Example for size 5:
5
55
555
5555
55555
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
#include<iostream>
using namespace std;
int main() {
int i, j, k, n, ch;
do {
cout << "\nMenu";
cout << "\n1. Square pattern";
cout << "\n2. Triangle pattern";
cout << "\n3. Diaginal pattern";
cout << "\n4. Reverse diaginal pattern";
cout << "\n5. Quit";
cin >> ch;
switch (ch) {
case 1:
a1:
cout << "\nEnter Size:";
cin >> n;
if (n >= 1 && n <= 9) {
for (i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
cout << n;
}
cout << "\n";
}
} else {
cout << "\nYour Size is Out of range!!";
goto a1;
}
break;
case 2:
a2:
cout << "\nEnter Size:";
cin >> n;
if (n >= 1 && n <= 9) {
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++) {
cout<<n;
}
}
} else {
cout << "\nYour Size is Out of range!!";
goto a2;} break; case 3:a3: cout<<"\nEnter Size:"; cin>>n; if(n>=1 && n<=9) { for(i=0;i<n;i++) { for(j=0;j<i;j++) { cout<<"*"; } cout<<n; for(j=i+1;j<n;j++) { cout<<"*"; } cout<<"\n"; } } else { cout<<"\nYour Size is Out of range!!"; goto a3;} break; case 4:a4: cout<<"\nEnter Size:"; cin>>n; if(n>=1 && n<=9) { for(i=0;i<n;i++) { for(j=1;j<n-i;j++) { cout<<"*"; } cout<<n; for(j=0;j<i;j++) { cout<<"*"; } cout<<"\n"; } } else { cout<<"\nYour Size is Out of range!!"; goto a4;} break; case 5: return 0; break; default: return 0; } }while(ch>0 && ch< 5); return 0; }
Comments
Leave a comment