#include <iostream>
double squareRoot(double number)
{
constdouble ACCURACY = .0000001;
double lower, upper, guess;
if (number < 1)
{
lower = number;
upper = 1;
}
else
{
lower = 1;
upper = number;
}
while ((upper - lower) > ACCURACY)
{
guess = (upper + lower) / 2;
if (guess*guess > number)
upper = guess;
else
lower = guess;
}
return (lower + upper) / 2;
}
int main(){
double value;
while (value != 0.0 || value < 0.0)
{
do {
cout << "Enter a number from 1 to 1000: ";
cin >> value;
if (value < 0.0 || value > 1000.0)
cout << "Number is not in proper range!" << endl;
else
cout << "Square root of " << value << " is " << squareRoot(value) << endl;
} while (value < 0.0 || value > 1000.0);
cout << "Enter a number from 1 to 1000: " << endl;
cin >> value;
cout << "Square root of " << value << " is " << squareRoot(value) << endl;
}
cout << "Loop exiting! Goodbye!" << endl;
return 0;
}
Output:
Enter a number from 1 to 1000: 200.0
Square root of 200 is 14.1421
Enter a number from 1 to 1000:
-6.0
Square root of -6 is -6 //why is it doing this, it's supposed to display "Value not in range"
Enter a number from 1 to 1000: 2000.0
Number is not in proper range!
Square root of 0 is 2.98023e-08 //why is it trying to calculate this, supposed to output Loop exiting! Goodbye! when it encounters a zero
Enter a number from 1 to 1000:
Square root of 0 is 2.98023e-08
Loop exiting! Goodbye!
never mind, it seems to screw up around the do while loop and letting some numbers go through depending on my amount of input, but that makes no sense, which leads me to believe that I have the syntax wrong
Notice that your first while loop while (value != 0.0 || value < 0.0) does absolutely nothing on the first iteration since value isn't initialized. I would take put a cin before the loop