Write a program that asks the user to enter the width and the length of the rectangle that the user wants to create.
Display:
Enter a length between 3 and 20: 21
You entered a non valid input. Try again
Enter a length between 3 and 20: 4
Enter a width between 3 and 20: 0
You entered a non valid input. Try again
Enter a width between 3 and 20: 5
*****
* *
* *
*****
Do you want to try again?(y/n) :y
Enter a length between 3 and 20: 7
Enter a width between 3 and 20: 12
************
* *
* *
* *
* *
* *
************
Do you want to try again?(y/n) :n
Code:
#include
using namespace std;
int main ()
{
//declaration of variables
int len, wid;
char choice;
//Using do while loop to ask the user again if he or she wants to try the program again
do
{
cout<< "Enter a length between 3 and 20: ";
cin >> len;
cout << endl;
while(len < 3 || len > 20 )
{
cout <<"\n\nYou entered a non valid input. Try again";
cout<< "\nEnter a length between 3 and 20: ";
cin >> len;
cout << endl;
}
cout << "Enter a width between 3 and 20: ";
cin >> wid;
while(wid < 3 || wid > 20)
{
cout <<"\n\nYou entered a non valid input. Try again";
cout<< "\nEnter a width between 3 and 20: ";
cin >> wid;
cout << endl;
}
int i=1,j=1,k=1,m=1,l=1;
for(i=1;i<=wid;i++)
cout<<"*";
for(j=1;j<=len-2;j++)
{
cout< for(k=1;k<=wid-2;k++)
cout <<" ";
for(l=1;l<=wid-k;l++)
cout<<"*";
}
cout
cout <<"\nDo you want to try again?(y/n) :";
cin >> choice;
}
while(choice == 'y' || choice =='Y');
return 0;
}