我想要好处,但它只显示0 [英] i want the benefit but it just show the 0

查看:58
本文介绍了我想要好处,但它只显示0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// saham.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<queue>
#include<iostream>
using namespace std;

int main()
{

  queue<int> boughtn ;
  queue<int> soldn ;
  queue<int> boughtp ;
  queue<int> soldp ;
  int boughtnum ;
  int boughtprice ;
  int soldnum ;
  int soldprice ;
  int benefit = 0 ;
  cout << "Please enter number of bought stocks (enter 0 to end):\n" << endl ;
 
  do
  {
    cin >> boughtnum ;
    boughtn.push (boughtnum);

  }
  while (boughtnum);

   cout << "Please enter price of bought stocks (enter 0 to end):\n" << endl ;
 
  do
  {
    cin >> boughtprice ;
    boughtp.push (boughtprice);
  }
  while (boughtprice);

  cout << "Please enter number of sold stocks (enter 0 to end):\n" << endl ;
 
  do
  {
    cin >> soldnum ;
    soldn.push (soldnum);
  }
  while (soldnum);

   cout << "Please enter price of sold stocks (enter 0 to end):\n" << endl ;
 
  do
  {
    cin >> soldprice ;
    soldp.push (soldprice);
  }
  while (soldprice);
  

  while ( ( !boughtn.empty() ) & ( !soldn.empty() ) )
  {
	   boughtn.front();
	   boughtn.pop() ;
	   boughtp.front();
	   boughtp.pop() ;
	   soldp.front();
	   soldn.pop() ;
	   soldn.front();
	   soldp.pop() ;
	  
	
	  //benefit += soldn.pop() * ( soldp.pop() - boughtp.front());
	  boughtnum = boughtnum - soldnum ; 
	  benefit = benefit + ( boughtprice - soldprice ) * soldnum ;
 
   cout << "The benefit is " << benefit << endl ;
  }

    


}

推荐答案

http: //www.cplusplus.com/reference/stl/queue/pop/ [ ^ ]

你一直叫流行音乐.但是,您永远不会调用front()将值放在队列顶部.您要累加的数字永远不会分配任何值.
http://www.cplusplus.com/reference/stl/queue/pop/[^]

You keep calling pop. But, you never call front() to get the value on top of the queue. The numbers you''re adding up, never get any value assigned to them.


您需要查看设置几个数组:一个用于数量,一个用于价格等
目前,您正在读取值,并覆盖前一个值.因此,当您处理数字时,只有用于终止输入的零.


忽略这一点,我只是看到您正在使用队列-我需要更多咖啡!
You need to look at setting up a couple of arrays: one for quantity, one for price, etc.
At the moment, you are reading the value, and overwriting the previous one. So when you come to process your numbers, you only have the zeros you used to terminate input.


Ignore that, I just saw you are using queues - I need more Coffee!


建议:
为什么不创建一个结构变量并将该值传递给队列,而不是创建n个队列.

例子:
Suggestion:
Why don''t you create one struct variable and pass that value to queue, instead of creating a n number of queue.

Example :
typedef struct _STOCKINFO
{
   int boughtn ;
   int soldn ;
   int boughtp ;
   int soldp ;

}STOCKINFO, *pSTOCKINFO;

typedef vector<stockinfo> VECSTOCKINFO;
////////////////////////////
STOCKINFO stStockInfo;
VECSTOCKINFO vecStockInfo;
stStockInfo.boughtn = 10;
stStockInfo.soldn = 10;
stStockInfo.boughtp = 10;
stStockInfo.soldp = 10;
vecStockInfo.push(stStockInfo);
</stockinfo>


这篇关于我想要好处,但它只显示0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆