当代码运行时,FtoC()
函数中的cin
函数被忽略,而ctemp
的值默认为0
。我已经得到了代码运行期望使用其他代码(不同的循环),但我真的很想了解这种错误的机制,并得到这样做的工作。Cin不会暂停(忽略)用户输入
#include <cstdlib>
#include <iostream>
using namespace std;
void threeint();
void FtoC();
int main()
{
threeint();
FtoC();
return 0;
}
void FtoC()
{
double ctemp = 0, ftemp = 0;
cout << "Please enter the temperature in Celsius which you would like to be\
converted to Fharenheit." << endl;
cin >> ctemp;
ftemp = ((ctemp * (9/5)) + 35);
cout << ctemp << " degrees celsius is " << ftemp << " in fahrenheit" << endl;
}
void threeint()
{
int x = 0, bigint = 0, smlint = INT_MAX, avgint = 0, index = 0;
cout << "Input as many integers as you like and finalise by entering any
non-integer input" << endl;
while (cin >> x)
{
if (x > bigint)
bigint = x;
if (x < smlint)
smlint = x;
++index;
avgint += x;
}
cout << "The largest integer is " << bigint << ".\t" << "The smallest
integer is " << smlint << ".\t";
cout << "The average of all input is " << (avgint/index) << endl;
}
如果'double'或'int'提取失败,您从不检查'cin'的状态。 –
无关但是(9/5)'不会做你认为它做的事。 (提示:那个结果恰好是1,如果你感到惊讶,[见这里](http://mathworld.wolfram.com/IntegerDivision.html)) – Borgleader