I have this code:
#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;
}
and it would show this result:
Square Root Function
Enter a number = 12
the square root is: 3.4641
but now the code is showing these results:
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
It seems like the cout will only show up first if an endl was added after the string. This just started happening recently. Is there a way I can fix this to show the proper output?