【发布时间】:2014-01-17 03:59:39
【问题描述】:
这里是sn-p的代码:
#include <iostream>
using namespace std;
int main ()
{
double degree;
do {
cout << "Enter a temperature in degrees Celsius: ";
cin >> degree;
} while (cin.fail());
//reassigns degree to conversion to farenheit
degree = degree * (9/5) + 32;
cout << "Your temperature in degrees Farenheit is: " << degree;
return 0;
}
如果输入无效,程序将进入无限循环,不断重复第一个 cout。
我对 C++ 有点陌生,我不确定这是否只是编译器的行为不正常,还是我自己的问题。
【问题讨论】:
-
你不想
cin.eof()吗? -
eof() 函数有什么作用?
-
它在我链接的页面上解释了这一点。
-
不直接相关,但您很快就会注意到
(9/5)与1完全相同(因为整数除法忽略余数)。可能最简单的方法是输入1.8。
标签: c++ loops infinite-loop