C++将用户输入传递给变量,传递给函数的参数

标签 c++ parameters

在编程方面,我是个菜鸟,但我一直在观看 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/

相关文章:

azure - 如何在 Azure 数据工厂中的相对 URL 中使用动态内容

c++ - 如何用按位运算实现位 vector ?

c++ - 为回调用例传递的成员函数指针

c++ - 代码在通常必须调用 move ctor 的地方调用 copy ctor

c++ - 2 插槽系统上的 OpenMP

c++ - 纯右值的结果

sql - 通过表触发器捕获存储过程参数

javascript - 语法错误: Unexpected token o in JSON at position 1 EXPRESS

moq - 使用 Moq 中的 Delegate 参数验证方法

C++14 在方法定义中使用 auto 关键字