The problem is that everytime I run the compile program the output appears to be 1+0+0+0+0....all the way to infinity (I have to manually close the command prompt window).
I've looked over the code and can't seem to pinpoint what's causing this to happen. I'd be really grateful if someone could help me
Also the ^ in the answer formula should be replaced by the pow() functon which requires the
<cmath> header file. .......or just i*i*i*i......etc.
Also you haven`t included the first term in answer as far as i can see. Need ans+=.....etc.
First, 1 || 3 is true, since || is a logical-OR, and both 1 and 3, when treated as booleans are true. So true or true is true.
So the line of code is really
if( i = true )
which is also always true, since = is assignment, not comparison, and the value of ( i = true ) is true, since the value of any expression ( x = y ) is y in C++ for any POD-type.
Thanks for the explanation on the boolean, is there a way to combine if(i==either 1 or 3) in terms of the value of i instead of splitting it into if and else if as I did?
You can use the ||, but you can't make it that sort:
1 2 3
if(i == 1 || i == 3) {
//...
}
Although this won't be a problem for this code...you might want to use the built-in pow() instead of your own power function so you can do roots/decimal powers like 2^3.2.