-3

Code provided in my lecture

I'm very curious about how does the "term" variable work in the first line of code when it codes: double pi = 0.0, term;

I also am unable to run this code on VSCode IDE Error Code C4578

I can't seem to find an answer on Google, so I came here. On the error of running the code, I tried to change numbers (adding decimal places) because it says its a data conversion problem with abs(). I think its something with abs() only outputing int datatype, is there anyway to fix it?

Thank u so much if you are seeing this, its my first time coding ;)

Calvin M
  • 13
  • 1
  • 5
    Please attach your entire code (include `#include`s) as text, not screenshot. And the error message too. – HolyBlackCat Aug 20 '23 at 11:32
  • The errors shown are not for that, but on different lines: the first is a warning for `int places = 10.0;`. The second error is using the integer macro `abs` on a floating point value. – Weather Vane Aug 20 '23 at 11:33
  • 1
    The `.cpp` file names indicate C++ not C. – Weather Vane Aug 20 '23 at 11:34
  • In the future, always reduce a problem to a [mre]: Make a complete program, with a `main` routine, that reproduces the problem, and show the compiler error messages by pasting them into the question as text, not as an image. – Eric Postpischil Aug 20 '23 at 11:56
  • Thank you so much guys, I'm so stupid. Learn't many things from your replies tysm! – Calvin M Aug 21 '23 at 02:06

1 Answers1

0

In double pi = 0.0, term;, pi = 0.0 and term are separate items. Equivalent code is double pi = 0.0;, which defines pi to be a double and initializes it to zero, and double term;, which defines term and does not specify an initial value for it.

The error “'initializing': conversion from 'double' to 'int', possible loss of data'” is from the line int places = 10.0;, which specifies a double value, 10.0, for an int object, places. To eliminate the warning, change this to int places = 10;.

In C, abs is a function for the int type. The fact that this code uses it with a double operand, term, indicates this is C++ code, and the file name, L2.1-2.3.cpp, also indicates this is C++ code. You seem to have requested your compiler to compile it as C code, which is why it gives you the message “'abs': conversion from 'double' to 'int', possible loss of data”. You should change whatever setting is telling your compiler to compile this as C code.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312