我正在尝试学习 C++,但我正在尝试解决一个问题。基本上我需要计算一个数字的平方根。我认为我走在正确的轨道上,但是当我运行代码时,输入数字后什么也没有发生。
int n;
double r, intGuess, guess, ratio;
// user input
cout << "Enter number: ";
cin >> n;
intGuess = n;
guess = n / 2;
ratio = intGuess / guess;
while (ratio >= 1.01 || ratio <= 0.99)
{
r = n / guess;
guess = (guess + r) / 2;
}
cout << endl;
cout << "The square root of " << n << " is " << guess << endl;