【发布时间】:2018-03-05 08:32:46
【问题描述】:
我已经盯着这个看了大约一个小时,老实说,我不知道我错过了什么。
#include <iostream>
using namespace std;
void callChain();
double chain(int, int &, int &);
int main()
{
callChain();
}
void callChain() {
int totalInches = 53;
int feet;
int inches;
cout << "\nTesting chain for 53 inches: \nExpected result: 15.46 feet: 4 inches: 5" << endl;
cout << "Actual result: " << chain(totalInches, feet, inches) << " feet: " << feet << " inches: " << inches << endl;
}
double chain(int totalInches, int &feet, int &inches) {
feet = totalInches / 12;
inches = totalInches % 12;
return (feet) * 3.49 + (inches) * .30;
}
返回是正确的,所以显然该功能正在工作,但对于我来说,我无法弄清楚为什么英尺和英寸没有改变。一切都拼写正确,我有所有的 &
【问题讨论】:
-
未指定输出中发生的事情的顺序。您不知道
feet、inches和chain()函数调用的评估顺序。 -
检查这个问题及其答案:stackoverflow.com/q/8931249/5958455
-
为了说明事情的不确定性,我将您的代码逐字粘贴到 jdoodle for g++ 14 GCC 7.20 中,得到了您期望的答案。 :)
标签: c++