如何从A内部获取值的总和

How to get the sum of values from inside a for loop

本文关键字:获取 内部      更新时间:2023-10-16

我被困在一个程序上,该计划收集了用户对股票的买卖价格的投入,以及它们拥有多少股。我正在使用for循环根据他们希望处理多少选项来测试条件。每个循环后都保存利润值,但是随着循环退出,最终值仅输出。是否有一种方法可以将总计分配给循环内部特定单个变量并将它们打印出来的总数。

这是我的代码:

int _tmain(int argc, _TCHAR* argv[])
    {
double buy = 0;
double sell = 0;
double shares = 0;
int options = 0;
double profit = 0;
double totalProfit = 0;
double *pBuy = &buy;
double *pSell = &sell;
double *pShares = &shares;
double *pProfit = &profit;
double *ptotalProfit = &totalProfit;
cout << "Please enter the number of stock option to process:" << endl;
cin >> options;
for ( int i = 0; i < options; i++)
{
cout << "Please enter the buy price for stock #" << i + 1 <<":" << endl;
cout << "$"; cin >> *pBuy;
cout << "Please enter the sell price for stock #" << i + 1 << ":" << endl;
cout << "$"; cin >> *pSell;
cout << "Please enter the shares for the stock #" << i + 1 <<":" << endl;
cin >> *pShares;
*pProfit = (*pSell - *pBuy) * *pShares;
}
*pProfit = (*pSell - *pBuy) * *pShares;
cout << "Total Profit is:" << "$" << *pProfit << endl;
system("pause");
return 0;

}

我认为您想要以下内容。只需继续添加到profit变量。由于您不是通过指针或参考将此变量传递给另一个函数,因此不需要指针使用。

profit = 0;
for ( int i = 0; i < options; i++)
{
    cout << "Please enter the buy price for stock #" << i + 1 <<":" << endl;
    cout << "$"; cin >> buy;
    cout << "Please enter the sell price for stock #" << i + 1 << ":" << endl;
    cout << "$"; cin >> bell;
    cout << "Please enter the shares for the stock #" << i + 1 <<":" << endl;
    cin >> shares;
    profit += (sell - buy) * shares;
}
cout << "Total Profit is:" << "$" << profit << endl;

首先,您不必使用指针。这是不必要的。第二,您可以使用Total -Profit变量来概括利润。这是代码:

    int main()
    {
         double buy = 0;
         double sell = 0;
         double shares = 0;
         int options = 0;
         double profit = 0;
         double totalProfit = 0;
         cout << "Please enter the number of stock option to process:" << endl;
         cin >> options;
         for ( int i = 0; i < options; i++)
         {
               cout << "Please enter the buy price for stock #" << i + 1 <<":" << endl;
               cout << "$"; cin >>buy;
               cout << "Please enter the sell price for stock #" << i + 1 << ":" <<endl;
               cout << "$"; cin >> sell;
               cout << "Please enter the shares for the stock #" << i + 1 <<":" << endl;
               cin >> shares;
               profit = (sell - buy) * shares;
               totalProfit+=profit;
          }
         cout << "Total Profit is:" << "$" << totalProfit << endl;
    }

如果您想存储每日利润。您可以将它们存储在数组中。

    int main()
    {
         double buy = 0;
         double sell = 0;
         double shares = 0;
         int options = 0;
         double profit = 0;
         double totalProfit = 0;
         cout << "Please enter the number of stock option to process:" << endl;
         cin >> options;
         double profitArray[options];
         for ( int i = 0; i < options; i++)
         {
               cout << "Please enter the buy price for stock #" << i + 1 <<":" << endl;
               cout << "$"; cin >>buy;
               cout << "Please enter the sell price for stock #" << i + 1 << ":" <<endl;
               cout << "$"; cin >> sell;
               cout << "Please enter the shares for the stock #" << i + 1 <<":" << endl;
               cin >> shares;
               profit = (sell - buy) * shares;
               profitArray[i]=profit;
               totalProfit+=profit;
          }
         cout << "Total Profit is:" << "$" << totalProfit << endl;
    }