在编程方面,我是个菜鸟,但我一直在观看 YouTube 上的一些教程,因此我决定尝试混合使用不同视频中教授的一些概念。我想要做的是拥有一个包含 3 个参数的函数,用于计算股票市场投资的返回。在我的主要任务中,我想从用户那里获取函数的 3 个参数,并将每个参数存储在一个变量中,并在调用函数时将这些变量用作参数。我目前遇到一个错误,但在我输入全部内容之前,我会向你们展示我的代码,也许你们可以发现问题所在。
#include <iostream>
#include <cmath>
using namespace std;
float stockMarketCalculator(float p, float r, int t){
float a;
for(int day = 1; day <=t; day++){
a = p * pow(1+r, day);
cout << a << endl;
}
}
int main()
{
float p;
float r;
int t;
cout << "Please enter the principle" << endl;
cin >> p >> endl;
cout << "Please enter the rate" << endl;
cin >> r >> endl;
cout << "Please enter the time in days" << endl;
cin >> t >> endl;
cout << stockMarketCalculator(p, r, t);
return 0;
}
最佳答案
第一个错误:您的 stockMarketCalculator 函数应该返回值!
return a;
第二次错误(3次):endl中不需要cin。只需将其删除即可。
尽情享受吧!
关于C++将用户输入传递给变量,传递给函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13187021/