- #1
ineedhelpnow
- 651
- 0
ok so in this program, if three valid sides are given it displays the area of a triangle otherwise it will ask the user to re enter sides.
This code needs to be modified so that a user is required to keep entering the three side until they decide to stop. If the three sides entered make an invalid triangle, the user is required to re-enter the values until valid triangle is formed. Then the area is displayed. In other words, the output needs to look like this.
Enter three sides of a triangle: 0 1 2 Error!
Re-enter three sides of a triangle: -1 1 2 Error!
Re-enter three sides of a triangle: 3 4 5 => 6
Continue (y/n)? y
Enter three sides of a triangle: 1 1 2 Error!
Re-enter three sides of a triangle: 6 8 10 => 24
Continue (y/n)? n
Done!(P.S. "Continue (y/n)?" is only displayed after a valid triangle is formed. Otherwise, the user needs to re-enter sides until it's valid.)
i need help with this. any hints?
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double a,b,c=0;
double s,A=0;
cin>>a>>b>>c;
cout<<"Enter three sides: "<<a<<" "<<b<<" "<<c<<endl;
while(!(a<=0||b<=0||c<=0||((a+b)<=0)||((a+c)<=0)||((b+c)<=0))){
if((a>0)&&(b>0)&&(c>0)&&((a+b)>c)&&((a+c)>b)&&((b+c)>a)){
s=(a+b+c)/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<A;
}
else{
cout<<"Enter three sides: "<<a<<" "<<b<<" "<<c<<endl;
}
}
return 0;
}
This code needs to be modified so that a user is required to keep entering the three side until they decide to stop. If the three sides entered make an invalid triangle, the user is required to re-enter the values until valid triangle is formed. Then the area is displayed. In other words, the output needs to look like this.
Enter three sides of a triangle: 0 1 2 Error!
Re-enter three sides of a triangle: -1 1 2 Error!
Re-enter three sides of a triangle: 3 4 5 => 6
Continue (y/n)? y
Enter three sides of a triangle: 1 1 2 Error!
Re-enter three sides of a triangle: 6 8 10 => 24
Continue (y/n)? n
Done!(P.S. "Continue (y/n)?" is only displayed after a valid triangle is formed. Otherwise, the user needs to re-enter sides until it's valid.)
i need help with this. any hints?