void __fastcall TForm1::Button1Click(TObject *Sender) { double x, y, d; int xx, yy; d = d + 0.06; x = 400 + 100*cos(d); // строка не работает, почему? y = 400 + 100*sin(d); Canvas->MoveTo(100, 100); Canvas->LineTo(x, y); } 

Dear programmers, please explain to the student what he is wrong about? Thank you in advance!

  • Although the answers are accepted, or how ??? - 3JIoi_Hy6 September

3 answers 3

Is this, as I understand it, C ++ Builder? Shouldn't the cmath header be inserted?

Still pays attention:

 double d; d = d + 0.06; 

You perform an action with an uninitialized variable. It's not very good.

  • Yes, this is C ++ Builder 6, the header seems to be worth it! I do not understand what an uninitialized variable means? - yost
  • 3
    When you write something like double d; the compiler allocates space in memory for this variable. Local variables are located in such a thing as a stack. As long as you have not assigned a specific value to this variable (d = 4.4 or through an operation with another specific variable or constant), this cell contains what was in it before. And this can be anything, since the stack uses all functions and after that it is not cleared, the value of the register pointing to the top of the stack simply changes. - skegg
  • oo how difficult everything is, now it's clear to you)) - yost
  • Nothing complicated really. I recommend reading a little about the assembler, then everything will immediately become clear. - skegg
  • one
    Thank you - this is good. And put a plus on top? - skegg
 #include <cmath> #include <iostream> using namespace std; int main() { cout << "sin(3.14) = " << sin(3.14) << endl; return 0; } 

Conclusion.

sin(3.14) = 0.00159265

Total

  1. Connect title.
  2. Make sure the call is correct.
  • Oooh ... earned, thank you very much. I always programmed in Delphi, and here I decided to try C ++, I had no idea that I would have to plug in headers. Thanks again! - yost
 #include<cmath>//теперь должно работать void __fastcall TForm1::Button1Click(TObject *Sender) { double x=0, y=0, d=0;/*тут была ошибка-переменные заполнены "мусором"-тем, что было в памяти на тот момент*/ int xx, yy; d = d + 0.06; x = 400 + 100*cos(d); // строка не работает, почему? y = 400 + 100*sin(d);/*потому что вы не подключили cmath, который хранит описание sin(), cos(), M_PI и т.д*/ Canvas->MoveTo(100, 100); Canvas->LineTo(x, y); }