p124 题2.16
有位司机记录了自己每次行驶的路程和每次耗费的汽油量。求每次加油平均一加仑汽油行多少英里。输入-1时 ,求出综 合平均数.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
double miles;
double gallons;
double gallons_sum=0;
double miles_sum=0;
while (gallons !=-1.0){
cout<<"Enter the gallons used (-1 to end): ";
cin>>gallons;
if (gallons =-1.0)
break;
cout<<"Enter the miles driven: ";
cin>>miles;
cout<<"The average for this time was "<<miles/gallons<<endl;
gallons_sum+=gallons;
miles_sum+=miles;
}
cout<<"The total average is "<<miles_sum/gallons_sum<<endl;
return 0;
}
它总是跳出循环,我还没输-1呢。
[此贴子已经被作者于2006-2-22 14:14:45编辑过]