提问人:Meno 提问时间:11/8/2017 更新时间:11/8/2017 访问量:98
c++ 将 var 从一个函数传递到另一个函数,然后在 main 函数中显示结果
c++ Passing var from function to function then display result in main function
问:
如何将用户输入的从一个函数收集的距离传递到另一个函数以计算运费?然后通过 int main 函数显示成本?
我的距离函数
double getDistance(double distance)
{
cout << "Enter the distance to be shipped: (min 10 Mi, max 3000 Mi): ";
cin >> distance;
while(distance<=10 || distance>= 3000){
cout << "We do not ship less than 10 miles or more than 3000 miles,
please reenter: ";
cin >> distance;
}
return distance;
}
我在想我可以返回距离,然后在另一个功能中使用它??是吗?
我需要在另一个函数 calcCost 中计算最终的运输成本,然后在 main 函数 int main() 中简单地显示它 cout << “the cost is ” << finalCost << endl;
如何将可变距离传递给另一个函数来计算成本,然后通过 int main() 函数显示成本?
答:
-1赞
pTz
11/8/2017
#1
是的,您可以使用 main 函数中返回的 distance 值作为另一个函数的参数:
int main(){
distance= getDistance();
finalCost = calculatefinalCost(distance);
..
}
顺便说一句:你不需要getDistance中的参数,无论如何,该值都会被用户输入替换。
评论