This program is supposed to take the square root of a number without using the sqrt function. it keeps giving me a control may reach end of non void function error. I noted where at in the code it was giving me the error. Please help.
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<iomanip>
using namespace std;
const double epsilon=1e-10;
double squareroot (double n) {
if(n<0) {
cout<<sqrt(n)<<endl;
}
double low, high;
if (n > 1)
{
low = 1;
high = n;
}
else
{ low = 0;
high = 1;}
double mid = low + (high - low)/2;
while (low <= high){
cout<<fixed;
cout<<setprecision(8)<<low<<"\t"<<setprecision(8)<<high<<"\t"<<setprecision(8)<<mid<<"\t"<<setprecision(8)<<mid*mid<<"\t"<<setprecision(8)<<(mid*mid-n)<<endl;
if (fabs(mid*mid-n)<epsilon){
return mid;}
else if (mid*mid < n){
low = mid;}
else {
high=mid;}
}
} This is where im getting the control error
int main ()
{
cout<<"Enter a number to find its square root -> ";
double n;
cin>>n;
double sq=squareroot(n);
cout<<fixed;
cout<<"sqrt("<<setprecision(6)<<n<<") "<<setprecision(6)<<sq<<endl;
return 0;
}
Please use the [code][/code] tags when posting code.
> } This is where im getting the control error
It means the compiler thinks there is a path through your code which doesn't result in reaching the return mid; statement.