【问题标题】:function does not take 1 arguments c++函数不接受 1 个参数 c++
【发布时间】:2014-03-05 17:40:24
【问题描述】:

我的代码有问题,因为我无法弄清楚为什么会收到错误消息。代码如下:

using namespace std;

void presentValue();
bool stringChar();
bool stringVal();
double futureValConv();

int main()
{
  cout << "Welcome to the Present Value Interest Calculator!\n\"First, let me collect some data." << endl << endl;
  presentValue();
  return 0;
}

void presentValue()
{
  //declare variables
  //Response value intialized as x for debugging
  char response = 'x';

  while (response != 'n' || response != 'N')
  {
    //declare variables
    double intRate = 0;
    string futureValString;
    double futureVal;
    double years = 0;

    //Simple present value equation
    double presentVal = futureVal / pow((intRate + 1), years);

    cout << "What's your Interest Rate?  ";
    cin >> intRate;
    cout << "OK, and what's your desired Future Value? [H}elp  ";
    cin >> futureVal;
    //Run descending help program that won't allow escape without a double value
    **futureVal = futureValConv(futureValString);**
    cout << endl << endl << "And finally, how many years would you like to save your money for?  ";
    cin >> years;
    cout << endl << "You've made it this far!!!";
    cout << endl << endl << presentVal;

  }
}
inline double futureValConv(string somestring)
{
  //delcare variables
  double newString = 0;

  **if (stringChar(somestring))**
  {
    cout << endl << "Future Value is the amount you would like to have in your account in the future.\n\n";
    cout << "How much might that be?  ";
    cin >> somestring;
    futureValConv(somestring);

  }
  **else if(stringVal(somestring))**
  {
    //Convert the Future Value String to a double for future use
    newString = atoi(somestring.c_str());
  }  
  else
  {
    cout << "Please enter a proper value.  ";
    futureValConv(somestring);
  }

  return newString;
}

bool stringChar(string response)
{
  //declare variables
  char answer = response[0];
  bool status = false;

  if (answer == 'H' || answer == 'h')
  {
    status = true;
    return status;
  }
}

bool stringVal(string response)
{
  //declare varialbes
  int answer = atoi(response.c_str());
  bool status = false;
  int powZero = (answer, 0);

  if (powZero == 1)
  {
    status = true;
    return status;
  }

}

我发布了我的大部分代码,因为我无法理解这里发生了什么。它告诉我 stringChar 和 stringVal 不带 1 个参数,以及 futureValConv。我正在尝试运行一个函数来检查字符串的值,并在做出决定之前确定该值是什么。在三个实例中的两个实例中,函数调用自身再次运行,直到用户输入可接受的双精度数(我通过运行 0 的幂来检查结果是否为 1。)我已将生成错误的三行加粗.有趣的是,即使注释掉函数 futureValConv 并且从不调用其他函数,我仍然得到三个错误中的两个。

【问题讨论】:

    标签: c++ function boolean return


    【解决方案1】:

    您在顶部声明了这一点:

    bool stringChar();
    bool stringVal();
    

    所以编译器期望 stringChar 和 stringVal 函数没有参数。 将声明更改为:

    bool stringChar(string response);
    bool stringVal(string response);
    

    【讨论】:

    • 特别说明:这不是C,空括号表示参数个数未知。
    • 显然我应该在实现函数原型之前多了解一点。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多