如何从for循环中获取值的总和

时间:2014-02-05 03:40:37

标签: c++ visual-c++

我被困在一个程序上,该程序收集用户关于股票买卖价格和股票数量的信息。我正在使用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;

}

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

首先,您不必使用指针。这是不必要的。其次,您可以使用totalProfit变量来计算利润。这是代码:

    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;
    }