C ++在循环中添加多个函数调用的总和

时间:2013-11-27 06:35:15

标签: c++ function

我一直致力于通过循环功能计算多个股票销售的总利润/亏损的任务,其中每个股票销售由用户输入。我做了大量的谷歌搜索无济于事。我能够在循环中使用该函数,但是我无法弄清楚如何从多个销售中添加利润/损失 - 而只是显示每个函数调用的利润/损失。我的函数的算法检查你是否手动添加每笔销售的总数,只是不清楚如何找到多个函数调用的总和。

以下是我想要输入的示例数据,该数据应显示$ 3324.00的总利润:

sale   numberOfShares   salePrice   salesCommission   purchasePrice   purchaseCommission
1      25               15          7.50              5               2.50
2      100              2.50        12                1.75            8
3      1000             5.10        51                2               20

到目前为止我的代码:

#include <iostream>
#include <iomanip>
using namespace std;

// Function prototype
double stockProfitFunction(double NS, double PP, double PC, double SP, double SC);


// Main Function
int main()
{
    // Format output
    cout << fixed << setprecision(2);

    // Initialize variables
    double profit,
        numberOfShares,
        salePrice,
        saleCommission,
        purchasePrice,
        purchaseComission;
    int numberOfSales;

    // Get # of sales from user
    cout << "Multiple Stock Profit Calculator\n--------------------------------\n\n";
    cout << "How many sales do you wish to enter?: ";
    cin >> numberOfSales;
    cout << endl;

    // Perform function in loop for number of sales
    for (int i = 1; i <= numberOfSales; i++)
    {
        system("cls");  // Clears screen

        cout << "Multiple Stock Profit Calculator\n";
        cout << "(Currently entering stock sale #" << i << ")\n----------------------------------\n";

        // Get information from user
        cout << "Enter number of shares: ";
        cin >> numberOfShares;

        cout << "Enter sale price: ";
        cin >> salePrice;

        cout << "Enter sales commission: ";
        cin >> saleCommission;

        cout << "Enter purchase price: ";
        cin >> purchasePrice;

        cout << "Enter purchase commission: ";
        cin >> purchaseComission;

        //Calcualtes profit with function
        profit = stockProfitFunction(numberOfShares, purchasePrice, purchaseComission, salePrice, saleCommission);


        // Display "profit" or "loss" depending on positive or negative value returned by function
        if (profit >= 0)
        {
            cout << "\n-----------------------\n";
            cout << "You earned a profit of: $" << profit << endl;
            cout << "(Press enter to input next sale)";
            cin.get();
            cin.get();
        }
        else
        {
            cout << "\n-----------------------\n";
            cout << "You had a loss of: $" << profit << endl;
            cout << "(Press enter to input next sale)";
            cin.get();
            cin.get();
        }
    }
    return 0;
}


// Stock Profit Function, returns profit
double stockProfitFunction(double NS, double PP, double PC, double SP, double SC)
{
    return ((NS * SP) - SC) - ((NS * PP) + PC);
}

谢谢你看看!

1 个答案:

答案 0 :(得分:3)

  1. 将变量初始化为零。

  2. 每次计算利润时,请将其添加到该变量中。

  3. 如果需要,输出该变量的值。

  4. 顺便说一下:

    system("cls");  // Clears screen
    

    这是一个非常糟糕的习惯。也许在你的机器上,cls清除了屏幕,但是你无法知道cls命令可能在别人的机器上做什么。 (在我看来,没有名为cls的命令,clear screen命令是clear。)除非你绝对别无选择,否则你应该强烈避免在你的C ++代码中使用system。 / p>