【发布时间】:2020-06-05 04:04:54
【问题描述】:
我正在创建一个程序来计算平均加速度。它工作得很好,但我似乎无法获得加速度的值。谁能教我为什么会发生这种情况以及如何解决这个问题? 求帮助
#include <iostream>
using namespace std;
//--------------------------------------------------------------------------//
void Getdata(double &Vstart, double &Vend, double &time);
void calculate(double &Vstart, double &Vend, double &time, double &accel);
void Displayoutput(double &out);
//--------------------------------------------------------------------------//
int main() {
double Vstart = 0.0;
double Vend = 0.0;
double time = 0.0;
double accel,out = 0;
cout << "please enter your velocity(Vo=m/s)\n,velocity(Vt=m/s)\nand time(s=second)\n\n";
Getdata(Vstart, Vend, time);
calculate(Vstart, Vend, time, accel);
Displayoutput(out);
return 0;
}
//--------------------------------------------------------------------------//
void Getdata(double &Vstart, double &Vend, double &time) {
cin >> Vstart;
cin >> Vend;
cin >> time;
cout << "your Vo=" << Vstart << " ,Vt=" << Vend << " and T=" << time << "\n\n";
}
//--------------------------------------------------------------------------//
void calculate(double &Vstart, double &Vend, double &time, double &accel) {
accel = (Vstart - Vend) / time;
}
//--------------------------------------------------------------------------//
void Displayoutput(double &out) {
cout << "the acceleration =" << out;
}
//--------------------------------------------------------------------------//
【问题讨论】:
-
您打印了
out的值,该值被分配为0,此后从未更改。您不会打印accel的计算值。
标签: c++ pass-by-reference