Could you please help me with this c++ problem. Only the first task works, 2nd,
ID: 3846632 • Letter: C
Question
Could you please help me with this c++ problem. Only the first task works, 2nd, 3th, 4th are not working well.
#include<iostream>
using namespace std;
int main()
{
int i=0;
int height=0,width=0;
int p=0,k=0,x=0,j=0,y=0,i1=0;
int t=0;
while(i!=5)
{
cout<<"1. Filled Rectangle 2. Empty Rectangle 3. Filled Triangle 4. Empty Triangle 5. Exit";
cout<<" Enter option: ";
cin>>i;
if(i==1 || i==2)
{
cout<<"Enter height: ";
cin>>height;
cout<<"Enter width: ";
cin>>width;
if(i==1)
{
cout<<endl;
for(k=1; k<=height;k++)
{
for(p=1;p<=width;p++)
{cout<<"* ";
}
cout<<endl;
}
cout<<endl;
}
if(i==2)
{
cout<<endl;
for(k=1; k<=height;k++)
{
for(p=1;p<=width;p++)
{
if(k==1 || k==height)
cout<<"* ";
else
{
if(p==1 || p==width)
cout<<"* ";
else
cout<<" ";
}
}
cout<<endl;
}
cout<<endl;
}
}
if(i==3 || i==4)
{
cout<<"Enter height: ";
cin>>height;
k=0;y=0;i1=0;
if(i==3)
{
cout<<endl;
for(i1=1; i1<=height; ++i1, k=0)
{
for(y=1; y<=height-i1; ++y)
{
cout<<" ";
}
while(k != 2*i1-1)
{
cout<<"* ";
++k;
}
cout<<endl;
}
cout<<endl;
}
if(i==4)
{
cout<<endl;
for(i1=1; i1<=height; ++i1, k=0)
{
for(y=1; y<=height-i1; ++y)
{
cout<<" ";
}
while(k != 2*i1-1)
{
if(k==(2*i1-2) || k==0 || i1==height)
cout<<"* ";
else
cout<<" ";
++k;
}
cout<<endl;
}
cout<<endl;
}
}
if(i==5)
cout<<"Good bye!";
}
return 0;
}
Explanation / Answer
Updated the code:
#include<iostream>
using namespace std;
int main()
{
int i=0;
int height=0,width=0;
int p=0,k=0,x=0,j=0,y=0,i1=0;
int t=0;
while(i!=5)
{
cout<<"1. Filled Rectangle 2. Empty Rectangle 3. Filled Triangle 4. Empty Triangle 5. Exit";
cout<<" Enter option: ";
cin>>i;
if(i==1 || i==2)
{
cout<<"Enter height: ";
cin>>height;
cout<<"Enter width: ";
cin>>width;
if(i==1)
{
cout<<endl;
for(k=1; k<=height;k++)
{
for(p=1;p<=width;p++)
{cout<<"* ";
}
cout<<endl;
}
cout<<endl;
}
if(i==2)
{
cout<<endl;
for(k=1; k<=height; k++)
{
for(p=1;p<=width;p++)
{
//printing sides of rectangle
if(k==1 || k==height )
cout<<"* ";
else if(p==1 || p==width)
cout<<"* ";
else
cout<<" ";//double spaces
}
cout<<endl;
}
cout<<endl;
}
}
if(i==3 || i==4)
{
cout<<"Enter height: ";
cin>>height;
k=0;y=0;i1=0;
if(i==3)
{
cout<<endl;
for(i1=1; i1<=height; ++i1, k=0)
{
for(y=1; y<=height-i1; ++y)
{
cout<<" ";
}
while(k != 2*i1-1)
{
cout<<"* ";
++k;
}
cout<<endl;
}
cout<<endl;
}
if(i==4)
{
cout<<endl;
for(i1=1; i1<=height; ++i1, k=0)
{
for(y=1; y<=height-i1; ++y)
{
cout<<" ";//double spaces
}
while(k != 2*i1-1)
{
if(k==(2*i1-2) || k==0 || i1==height)
cout<<"* ";
else
cout<<" "; //double spaces
++k;
}
cout<<endl;
}
cout<<endl;
}
}
if(i==5){//terminating condition
cout<<"Good bye!";
break;
}
}
return 0;
}
Output:
Hope this helps you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.