HEY, I NEED HELP =(

Write your question here.
hi ,I wrote this code and I don't knew what is wrong
in the output the result is always ZERO ...
Is it the library ??
I could use some help =(
1
2
3
4
5
6
7
8
9
10
11
12
  #include<iostream>
using namespace std;
int main () 
{ int x, result; 
cout<<"Evaluate: 1/(x^2+x+1)\n";
cout<<"ENTER THE VALUE OF X :";
cin>>x;
result=1/(x*x+x+1) ;
cout<<"1/(x^2+x+1) = "<<result;
cout<<endl; 
return 0;
}
i think it should be this result=1/((x*x)+x+1)

and the data type for x should be double not int
closed account (E0p9LyTq)
You are getting the correct answer, 0, when dealing with integer math.

Change your two variables, x and result, to either float or double.
Hi,

That is because of integer division, an int cannot hold a fractional part, so it truncates it. Change the type of your variables to double.

Also, it's always a good idea to check for division by zero. With double, check that the number is not less than some precision value like 0.00001 say. One cannot do a direct comparison with zero for doubles, have a read on Google as to why that is.

Even though the function may be strictly positive, it's worth doing a check anyway, it might save you one day if the math function is changed to something else that might produce 0.

Good luck :+)
@ayanamirei2011

The extra parentheses aren't necessary, but do convey a better meaning.
THANK YOU ALL
Topic archived. No new replies allowed.