如何从A内部获取值的总和
How to get the sum of values from inside a for loop
我被困在一个程序上,该计划收集了用户对股票的买卖价格的投入,以及它们拥有多少股。我正在使用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;
}
相关文章:
- Boost Spirit,获取迭代器内部语义动作
- 如何在pugixml中获取节点的内部XML
- 从 qmake 运行脚本以获取内部版本号
- 如何在函数内部使用 zmq(以非阻塞方式)在客户端请求时获取函数的状态?
- 如何通过从变量中获取类型来访问内部 typedef
- ssl3_read_bytes:TLSV1警报内部错误,安全套接字,OpenSSL,C ,获取HTTPS页面
- 获取IntelliSense信息与扩展中的内部
- 如何从对象内部的 typedef 获取模板参数类型
- 如何获取类内部的结构?
- 获取指向自我内部班级的指针
- 从操作员内部获取有关对象的RTTI信息
- 如何从A内部获取值的总和
- 从C++内部获取损坏的符号名称
- 如何获取有关结构/类内部"current type"的信息?
- 如何从派生的小部件类内部获取父 Gtk::Window
- 从直接调用的静态方法内部获取类对象指针
- 如何在C++内部的Google Native Client中获取数组中的值的总和
- 从Vector内部的结构中获取浮点值
- 如何获取方法内部int数组(方法参数)的长度
- 正在获取成员函数内部成员函数的地址