2017-06-17 17 views
-3

私は関数から変数を渡す方法を理解していますが、複数の関数を一緒に追加しようとしていますか?関数に格納されているdouble値をすべてtotalBillAmountという1つの関数に追加したいと思います。 (ネイビークレジットカード+ wfスーシティカード+ ....など)。また、それらはすべて異なる二重の値を持っていても、関数を組み合わせる方法はありますか? (つまり、手形と呼ばれる一つの関数を作るために、すべての法案を組み合わせる)複数の関数を1つに追加するにはどうすればよいですか?

//this is my main file 
Budget budget; 

budget.introText(); 
double inputNavyCreditCardBill = budget.navyCreditBill(); 
double inputWellsStudentLoanBill = budget.wfStudentBill(); 
double inputSprintPhoneBill = budget.sprintBill(); 
double carBill = budget.carBill(); 
double capitalOneCreditCard = budget.capitalCredit(); 
double fedStudentLoan = budget.fedCreditBill(); 
double navyPersonalLoan = budget.navyPersonalLoan(); 
double totalMoneyOnHand = budget.totalMoneyOnHand(); 
double totalBillAmount = 

    double moneyRemaining = totalMoneyOnHand - totalBillAmount; 
    std::cout << std::endl << "Money Remaining: $" << moneyRemaining << std::endl; 

    double extraMoneyTowardsDebt = moneyRemaining * .7; 
    std::cout << std::endl << "Extra Money For Debt: $" << extraMoneyTowardsDebt << std::endl; 

    double moneyFromCreditForGirlFriend = moneyRemaining * .3; 
    std::cout << "Extra Money From Credit For Girlfriend!: $" << moneyFromCreditForGirlFriend << std::endl; 

    double extraMoneyForMe = moneyRemaining * .3; 
    std::cout << "MA MONEY BITCH: $" << extraMoneyForMe << std::endl; 
return 0; 
} 
+3

親切(https://stackoverflow.com/help/mcve)[、最小完全、かつ検証]のルールに従うと、あなたの質問は、より多くので更新しますあなたの問題に関する情報そして**機能を追加することによって**あなたはどういう意味ですか? – Azeem

+0

私のコードを更新しました。申し訳ありませんが、私はこのウェブサイトを初めて利用しています。私はtotalMoneyOnHandという別の関数を作成したいので、他のすべての関数に格納されている値を加算することで、 "totalMoneyOnHand"などの関数を追加したい(私はbill(つまり、carBill、sprintBill) –

+0

すべてのdownvotesの人のためにありがとう:)私はプログラミングやこのウェブサイトや何かのようなものを初めて知っているかのように来て..itsていないようにしてください! –

答えて

0

あなたは、次のような機能を持っているとしましょう:

void func1(int bar1) { ... } 
void func2(int bar2) { ... } 
void func3(int bar3) { ... } 

あなたはすべての新しい大きい方の内側にこれらすべての機能を組み合わせたい場合あなたは何をする必要がある各小機能が大きなものの引数リストを必要とすることに引数を渡すである:あなたのケースであなたも作るの戻り値を持っている

void bigFunc(int bar1, int bar2, int bar3) { 
    ... 
    func1(bar1); 
    func2(bar2); 
    func3(bar3); 
} 

今物事は少し複雑です。これらを使って何をしたいかによって、大きな関数の中で計算を行い、単一の結果を返すか、配列、構造体またはより精巧なコンテナを使用して別々の結果を格納します。

あなたのケースでは、単一の結果を返すだけなので、非常に簡単です。以下に、一連の機能(double sum(double, double, double)double pow(double, double)double sub(double, double))を使用した例を示します。あなたのシナリオにこれを適応させることができる必要があります:

// Use the arguments list of bigFunc to pass down the arguments that each of the smaller function requires 
void bigFunc(double arg1, double arg2, double arg3) { 

    // Use temporary variables to store the return value of each smaller function 
    double s = sum(arg1, arg2, arg3); 
    double p = pow(arg1, arg3); 
    double sb = sub(arg1, arg2); 

    // Do whatever you want we the results and return it 
    return s + p - sb; 
} 
+0

ありがとうございました!なぜ誰もがあなたのように私を落とす代わりに素敵になれないのです。 –

+0

LOLそれはあなたが尋ねた質問ではありませんでしたが、あなたがそれを投稿したやり方は内容によって賢明でした。それを投稿した人が「これはあなたが「結合する」という意味ですか? – rbaleksandar

関連する問題