Change main()
to int main()
. C++ requires a type specifier for all functions (especially main
).
To be honest, I'm not sure how you got it to compile without that.
Just as an additional "fyi", you could also make these two changes (but they're not necessary):
Change main()
to int main(int argc, char const *argv[])
. That's the full function header for main
, and it's what allows you to process command line arguments. Your program doesn't take any command line arguments though, so in this case a simple int main()
is clearer (and thus preferable).
Put return 0;
as the very last line in your main
function body. Whatever value your program returns when it concludes is what lets the operating system know whether your program succeeded or failed somehow. Technically, C++ will implicitly return 0
for you anyway if you don't do anything else and your program terminates normally. Still-and-yet, it's considered good style in some circles to explicitly return 0;
(or to #include <stdlib.h>
and return EXIT_SUCCESS;
) rather than to let a returning function fall off the end of the block.
EDIT:
In response to your edits and comments, the problem probably lies elsewhere. I would strongly suggest looking at the link in the comment to your question by Basile Starynkevitch. Floating-point arithmetic can be a tricky thing to get right.
Additionally, please state what input values you are using. This is a general practice anyway for whenever you are asking other people for help, but in this case it is especially important due to the aforementioned issues with floating-point arithmetic.