as in C ++ Builder to create in the button, an expression that calculates the area by the Heron formula, that is, exactly the code, tried

void __fastcall TForm1::Button1Click(TObject *Sender) { int storona1, storona2, storona3; float plowad,p; storona1=StrToInt(Edit1->Text); storona2=StrToInt(Edit2->Text); storona3=StrToInt(Edit4->Text); p=(storona1+storona2+storona3)/2.0; plowad=sqrt(p*(p-storona1)*(p-storona2)*(p-storona3)); Edit3->Text=FloatToStr(plowad); } 
  • 1. learn to declare variables correctly 2. 2.0 and 2 are literals of different types. Dividing an int by an int strangely enough gives int. You need a real number 3. If you want to learn C ++, then change the IDE and compiler to something more normal, otherwise you will program it with the mouse using the form with buttons 4. Try to give meaningful English names to variables. However, this is just a wish - DreamChild
  • I'd love to just do it on him - parol
  • at the institute or what? - DreamChild
  • at the university - parol
  • it means that you are unlucky with the teacher (or even with the university) Borland C ++ (and its descendants) is a very doubtful choice for studying C / C ++ However, this is all unimportant. I hope you understand what is wrong with your code? - DreamChild

1 answer 1

good, in order.

a) you have an error in the line

 plowad=float sqrt(p*(p-storona1)*(p-storona2)*(p-storona3)); 

the mistake is that the word float is superfluous here.

b) the variable is declared as powad, and you write plowad, this is probably just a typo

c) the next error in the line

 p=(storona1+storona2+storona3)/2 

The fact is that both the numerator and the denominator are int. And when you divide int by int, you also get an integer in the response, even if there should be a remainder. For example: 5/2 from the point of view of mathematics is equal to 2.5 and from the point of view of C it is equal to two. Therefore, to get the correct result, you need to write this:

 p = (storona1 + storona2 + storona3) / 2.0 // 2.0 - это уже число с плавающей точкой, поэтому в результате вы получите тоже число с плавающей точкой 

either so:

 p = (float)(storona1 + storona2 + storona3) / 2 

d) Not a mistake, but simply a wish for the future. Do not write variables translit. This is a very bad practice. It is better to call in English words, at the same time knowledge of the language tighten, will not be superfluous. And I agree with @VladD - no need to push all the calculations in Button1Click. However, this is not good, you can understand by reading the link from Wikipedia. However, if your teacher is not completely a fool, then he should appreciate this when he sees that you have taken the logic out of the button handler into separate classes and / or functions.

  • some kind of error plowad = sqrt (p * (p-storona1) * (p-storona2) * (p-storona3)) - parol
  • plowad = float sqrt (p * (p-storona1) * (p-storona2) * (p-storona3)) - parol
  • plowad = sqrt (p * (p-storona1) * (p-storona2) * (p-storona3)) - parol
  • you missed * between the two brackets - DreamChild
  • I put it this way, but still something is wrong, I connected the cmath.h library - parol