【发布时间】:2017-04-12 15:16:46
【问题描述】:
我有这个代码:
#include <iostream>
using namespace std;
double sqrt(double n)
{
double x;
double y = 2; //first guess is half of the given number
for (int i = 0; i<50; i++)
{
if (n>0)
{
x = n / y;
y = (x + y) / 2;
}
if (n==0)
{
return 0;
}
}
return y;
}
int main()
{
cout << "Square Root Function" << endl;
double z=0;
while (true)
{
cout << "Enter a number = ";
cin >> z;
if (z<0)
{
cout<<"enter a positive number"<<endl;
continue;
}
cout <<"the square root is "<< sqrt(z) << endl;
}
return 0;
}
它会显示这个结果:
Square Root Function
Enter a number = 12
the square root is: 3.4641
但现在代码显示了这些结果:
Square Root Function
1 //my input
Enter a number = the square root is 1
2 //my input
Enter a number = the square root is 1.41421
似乎只有在字符串后面添加了 endl 时,cout 才会首先出现。这只是最近才开始发生的。有没有办法解决这个问题以显示正确的输出?
【问题讨论】:
-
问题数学是否与 cout 对象有关?
-
endl是一个换行符和一个刷新,所以你现在看到的似乎是合理的。