+ 1

Herons formula

The task is as follows: Enter the lengths of the triangles a, b, and c.  If three sides do not form a triangle, repeat the procedure (page entry, etc.), otherwise calculate using Heron formula the area of ​​a triangle and print it to one decimal place. My code seems so work fine, except the part where it should repeat the procedure if inputs do not form a triangle, instead it outputs 0 or nan. Not sure if its the problem in do-while loop or something else. #include <iostream> #include <math.h> #include <string.h> using namespace std; float heron (float, float, float); void triangle(){ float a, b, c; do { cin >> a; cin >> b; cin >> c; } while ((a+b<c) && (a+c<b) && (b+c<a)); ; cout << ceil((heron(a, b, c)*10))/10 << endl; } int main(){ triangle(); } float heron (float a, float b, float c){ float s=(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c)); }

16th Mar 2022, 1:06 PM
HackingPhotosynthesis
HackingPhotosynthesis - avatar
8 Answers
+ 1
first of all, sololearn never scan for input multiple times. all the inputs are scanned at the beginning (only one time). so never write a code in sololearn which is asking the user to input again /*************/ 2nd thing is that your condition inside while loop will never be going to true. So your while loop will never repeat again. and are you sure that your conditions are correct because to make a triangle you just needs 3 sides the condition is that the "s" of the 3 sides must be greater than each side. s > (a,b,c) otherwise your area will become negative. and squar root of negative number is an imaginary number which is same as Nan (i guessed) so if you want to write a condition it's better to compare your "s" with all sides. calculate s inside "do"
16th Mar 2022, 1:36 PM
NonStop CODING
NonStop CODING - avatar
+ 1
Manav Roy yeah! I know what are you saying👍 but I said " scan " for input multiple times not "input multiple times" you can't open the input terminal twice after executing the code. for example, after giving inputs you can't force the sololearn editor to open the input terminal again in the middle of your code execution.
16th Mar 2022, 4:10 PM
NonStop CODING
NonStop CODING - avatar
0
What's your triangle? Condition? edit: HackingPhotosynthesis use > instead of < and flip boolean try this , works fine I think. } while ( !( (a+b > c) && (a+c > b) && (b+c >a) ) );
16th Mar 2022, 1:34 PM
Jayakrishna 🇼🇳
0
FF9900 your code is also printing answers for input like a = 1, b = 2, c = 4 can it form a triangle ? How i don't think so it will make a triangle. try drawing on paper. You can never form a triangle with these value. your code should not be printing for these kinds of inputs because the value of s = 3.5 ohhk so in the formula you can see there is a term ( s - c ), so don't you think it will be negative. and because of square root you will get an imaginary number mathematically, which is simlilar to Nan (i guess)
16th Mar 2022, 2:28 PM
NonStop CODING
NonStop CODING - avatar
0
Manav Roy is it running for input like a=1, b=2, c=4 it should not run with these values because you can't form a triangle with them.
16th Mar 2022, 4:17 PM
NonStop CODING
NonStop CODING - avatar