You have not updated your y
variable while iteration.
Also using abs
is quite dangerous as it could round to integer on some compilers.
EDIT
To clarify what I've mean: using abs
with <math.h>
could cause implicit type conversion problems with different compiles (see comment below). And truly c++ style would be using the <cmath>
header as suggested in comments (thanks for that response).
The minimum changes to your code will be:
double CubicRoot(double x, double e)
{
double y = x;
double Ynew = x;
do
{
y = Ynew;
Ynew = y-((y*y)-(x/y))/((2*y)+(x/(y*y)));
cout << Ynew;
} while (fabs(Ynew-y)/y>=e);
return Ynew;
}