here's my complete code (untested, so you compile):
Code:
#include <iostream>
#include <cmath>
using namepace std;
int main (int argc, char **argv)
{
char quit = 'N';
double userinput, y, x, a, lasty; //All of our variables
while (toupper(quit) != 'Y')
{
cout << "Please enter a non-zero number *****:" << flush;
cin >> userinput; //Get the number to be square rooted
if (userinput <= 0) //Is it a nonvalid number?
{
cout << "The number you entered was invalid. Would you like to quit (y/n)?" << flush;
cin >> quit; //Does the user want to quit?
continue; //If he wants to quit, then quit. Else, continue.
}
a = userinput;
x = a;
/*********Square root loop*************/
lasty = 0; //Null out lasty
for (int i = 0; i <= 15; i++)
{
y = 0.5*(x+a/x); //This is our actual formula
x = y;
if (y == lasty)
{
break; //Our formula has reached its end prematurely (under 15 loops)
}
lasty = y
}
cout << "The square root is " << y << "." << endl;
cout << "Would you now like to quit (y/n)?" << flush;
cin >> quit;
}
cout << "Ending...." << endl;
}
I hope that helps you.