int main()
{
double x, y, z;
cout << "enter two numbers" << endl;
while (cin >> x >> y)
{
z = hmean(x, y);
cout << "harmonic mean of " << x << " and " << y << " is " << z << endl;
cout << "enter next set of numbers <q to quit> ;";
}
cin.clear();
cin.get();//请问下为什么加上这两行才可以在输入q离开第一个循环之后可以继续输入啊
cout << "Done." << endl;
cout << "------------------" << endl;
cout << "enter two other numbers" << endl;
double x1, y1, z1;
while (cin >> x1 >> y1)
{
if (hmean(x1, y1, &z1))
{
cout << "harmonic mean of .. " << x1 << " and " << y1 << " is " << z1 << endl;
}
else
{
cout << "one value should not be the negative" << " of the other - try again.\n";
}
}
while (cin.get() != '\n')
{
continue;
}
cout << "Done." << endl;
system("pause");
return 0;
}
double hmean(double a, double b)
{
if (a == -b)
{
cout << "untenable arguement to hmean()\n";
abort();
}
return 2.0 * a * b / (a + b);
}
bool hmean(double a, double b, double* ans)
{
if (a == -b)
{
*ans = DBL_MAX;
return false;
}
else
{
*ans = 2.0 * a * b / (a + b);
return true;
}
}
请问下为什么加上cin.clear()和cin.get()这两行之后才可以在输入q离开第一个循环之后可以继续输入啊