Im trying to access the objects in the vector array that ive created in the bid class. I need to access the first ask bid and then continue to compare the price and the quantity with the first buy bid. I dont understand how on EARTH im supposed access these elements from the simulator.Could someone please help!:(
Im lost at comparing and accessing:S
Heres the code:
#include<iostream>
using namespace std;
class Simulator
{
vector<Bid> BidList (20);
public:
void run();
void match();
};
void Simulator::run()
{
//my bad try at accessing the elements in the vector.completley wrong but i dunno why:s
for (int i; i<20; i++)
{
theBidlist(BidList);
BidList[i].show();
}
cout<<"Bid list after sort:"<<endl;
sort(BidList.begin(), BidList.end());
show2(BidList);
show3(BidList);
}
class Bid
{
private:
int bidID, quantity, price, traderid;
char tradertype;
public:
static int BidID;
Bid ( int tid, char ttype, int p , int q ) : traderid ( tid ), tradertype ( ttype ), quantity ( p ), price ( q )
{
bidID = Bid::BidID++;
}
//**************************************************
void show ( ostream &out ) const
{
out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
}
//***************************************
void show2 ( ostream &out ) const
{
if ( tradertype == 'A' )
out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
}
//********************************************
void show3 ( ostream &out ) const
{
if ( tradertype == 'B' )
out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
}
//********************************************
bool operator< ( const Bid &other ) const
{
return price < other.price;
}
};
int Bid:: BidID = 0;
//*******************Sets the values for the Bid List*********************************
Bid getBids()
{
int MinPrice = 50, MaxPrice = 150;
int range1 = ( MaxPrice - MinPrice ) + 1;
int MinQuantity = 30, MaxQuantity = 100;
int range2 = ( MaxQuantity - MinQuantity ) + 1;
int quantity = MinQuantity + int ( range1 * rand() / ( RAND_MAX + 1.0 ) );
int price = MinPrice + int ( range2 * rand() / ( RAND_MAX + 1.0 ) );
int traderid;
int NUMBIDS = 10;
char tradertype;
int bidID = Bid::BidID;
if ( bidID < NUMBIDS )
{
traderid = 0;
tradertype = 'A';
}
else
{
traderid = 1;
tradertype = 'B';
}
return Bid ( traderid, tradertype, quantity, price );
}
//*****************************************Adds the random values to the Vector*******************
void theBidlist ( vector<Bid> &list )
{
int numbids = 20;
for ( int i = 0;i < numbids;i++ )
{
list.push_back ( getBids() );
}
}
//*******************************************Scans through the vector****************************
void show ( const vector<Bid> &vect )
{
cout << "********************************************" << endl;
for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
{
sh->show ( cout );
}
cout << endl;
}
//****************************************************************************************************
void show2 ( const vector<Bid> &vect )
{
cout << "*******************Only Trader Ask*************************" << endl;
for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
{
sh->show2 ( cout );
}
cout << endl;
}
//**************************************************************************************************
void show3 ( const vector<Bid> &vect )
{
cout << "*******************Only Trader Buys*************************" << endl;
for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
{
sh->show3 ( cout );
}
cout << endl;
}