我需要编写一个程序,按照这些规则计算行驶距离的成本
到目前为止,这是我的程序
#include <iostream> //for cin >> and cout <<
using namespace std;
int main() // main algorithm
{
double distance;
double Milecost1;
double Milecost2;
double Milecost3;
double totalcost;
cout << "enter distance";
cin >> (distance);
void CFM();
if (distance >= 100) {
Milecost1 = 100 * 5.50;
} else {
totalcost = distance * 5.50;
}
void CSM();
if ( (distance >100) && (distance <= 500) ) {
Milecost2 = (distance - 100) *4.00;
}
void CTM();
if (distance > 500) {
Milecost3 = distance - 500 * 2.50;
}
void totalcost1();
totalcost = Milecost1 + Milecost2 + Milecost3;
cout << "the total cost for the distance travelled is" << totalcost
system("pause"); //to hold the output screen
return(0);
}
我的第一个问题是,算出费用正确吗?
第二个问题是我运行该程序,并说正在使用Milecost 2而不进行初始化,该如何解决呢?
最佳答案
不,数学不正确,例如使用distance = 501
,您将获得
Milecost1: 550
Milecost2: (unitialised)
Milecost3: 2.50
假设您纠正了
Milecost3
上的运算符优先级,因为现在您要乘以500乘以2.5并从distance
中减去它。仅当
Milecost2
在其相关值内时才分配distance
,相反,对于0
,它应该是distance <= 100
,并且在distance > 500
时也应该计算得出,如果我正确理解了该练习。关于c++ - 为什么这个简单的C++无法正常工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27078933/