如何 multiply/divide 一个功能到另一个?如何使用参数?

How to multiply/divide one function to another ? How to use parameters?

如何将一个函数乘以另一个函数?以及如何正确使用参数?

我不太确定,我真的是 C++ 的新手,只有大约 14 周的 class 时间。

我尝试过创建一个新函数来乘以其他函数,并在参数中放入函数名称。

例如:

float mealMath(numberOfAdults, mealChoosing){
   //Rest of function
}

但我总是得到 error.Please 解释如何解决这个问题,这对我来说是编程的一大障碍,我似乎无法掌握如何解决这个问题或着手做这些事情。别为了this.Thanks对我太苛刻了!

int numberOfAdults(){
    int totalAdults;
            cout << "Now how many adults will there be?: ";
            cin >> totalAdults;
                cout << "It seems there will be: " <<                      totalAdults << " Adults." << endl;
                    while(totalAdults < 1){
                        cout << "Sorry there has to be a minimum of 1 adult!" << endl;
                            cout << "How many adults: "; 
                                cin >>  totalAdults;
                    }
                    return 0;
}

int numberOfKids(){
    int totalKids;
        cout << "Now how many Kids will there be?: ";
            cin >> totalKids;
                cout << "It seems there will be: " <<   totalKids << " kids." << endl;
                    while(totalKids < 0){
                        cout << "Sorry there has   to be a minimum of 1 Kid!" << endl;
                            cout << "How many   Kids: "; 
                            cin >>   totalKids;
                    }
                    return 0;
                  }


float mealChoosing(){
    float cost;
        string mealChoise;
            cout << "   " << endl;
                cout << "Now, What meal will you be    getting(D/S): ";
                    cin >> mealChoise;
                        if(mealChoise == "D"){
                            cout << "It seems you have selected the Deluxe Meal plan for everyone!" << endl;
                                cost = 25.95;
                        }
                                    if(mealChoise == "S"){
                                        cout << "It seems you have selected the Standard Meal plan for everyone!" <<     endl;
                                            cost = 21.75;
                            }
                                                cout << "   " << endl;

                                                return cost;
}

一个预期的结果是我想将用户在函数 "numberOfAdults" 中提供的输入乘以用户在 "mealChoosing"

中提供的输入

所以我想要 numberOfAdults * mealChoosing 但我想在不同的函数中完成所以

"float total(){
   float totalBill;
     totalBill = numberOfAdults * mealChoosing;
       cout << totalBill;"

或类似的东西。我无法完成这个项目,因为出于某种原因我无法正确地为函数提供参数中所需的正确信息。

在这种情况下(以及大多数情况下),您不应声明其参数为函数的函数。而是使用整数和浮点输入声明 mealMath:

float mealMath(int a, float b){/*Your code here*/}

然后调用 mealMath 并将其他两个函数作为参数传递。

float x = mealMath(numberOfAdults(), mealChoosing());

或者,您可以没有 mealMath() 的函数参数,而是从函数内部调用 numberOfAdults()mealChoosing()

重要的是要注意,大多数时候您将调用一个函数并将其输出用作参数,因此您需要将 () 放在函数标识符之后,而不是只需单独输入标识符。

喜欢 mealChoosing() return totalAdultstotalKids(虽然这里不需要)分别来自 numberOfAdults()numberOfKids()

int numberOfAdults() {
  //...
  return totalAdults;
}

int numberOfKids() {
    //..
    return totalKids;
}

float mealChoosing() {
 //..

  return cost;
}

现在 mealMath(numberOfAdults, mealChoosing)

float mealMathOutput = mealMath(numberOfAdults(), mealChoosing());