Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// 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 ;
  }

    


}
Posted
Updated 29-Jun-11 21:38pm
v2

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.
 
Share this answer
 
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!
 
Share this answer
 
v2
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900