program not working as expected!
- by wilson88
Can anyone just help spot why my program is not returning the expected output.related to my previous question.Am passing a vector by refrence, I want to see whats in the container before I copy them to another loaction.if u remove comments on loadRage, u will see bids are generated by the trader.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int NUMSELLER = 1;
const int NUMBUYER = 1;
const int NUMBIDS = 20;
const int MINQUANTITY = 1;
const int MAXQUANTITY = 30;
const int MINPRICE =100;
const int MAXPRICE = 150;
int s=0;
int trdId;
// Bid, simple container for values
struct Bid {
int bidId, trdId, qty, price;
char type;
// for sort and find.
bool operator<(const Bid &other) const { return price < other.price; }
bool operator==(int bidId) const { return this->bidId == bidId; }
};
// alias to the list, make type consistent
typedef vector<Bid> BidList;
// this class generates bids!
class Trader {
private:
int nextBidId;
public:
Trader();
Bid getNextBid();
Bid getNextBid(char type);
// generate a number of bids
void loadRange(BidList &, int size);
void loadRange(BidList &, char type, int size);
void setVector();
};
Trader::Trader() : nextBidId(1) {}
#define RAND_RANGE(min, max) ((rand() % (max-min+1)) + min)
Bid Trader::getNextBid() {
char type = RAND_RANGE('A','B');
return getNextBid(type);
}
Bid Trader::getNextBid(char type) {
for(int i = 0; i < NUMSELLER+NUMBUYER; i++)
{
// int trdId = RAND_RANGE(1,9);
if (s<10){trdId=0;type='A';}
else {trdId=1;type='B';}
s++;
int qty = RAND_RANGE(MINQUANTITY, MAXQUANTITY);
int price = RAND_RANGE(MINPRICE, MAXPRICE);
Bid bid = {nextBidId++, trdId, qty, price, type};
return bid;
}
}
//void Trader::loadRange(BidList &list, int size) {
// for (int i=0; i<size; i++) { list.push_back(getNextBid()); }
//}
//
//void Trader::loadRange(BidList &list, char type, int size) {
// for (int i=0; i<size; i++) { list.push_back(getNextBid(type)); }
//}
//---------------------------AUCTIONEER-------------------------------------------
class Auctioneer {
vector<Auctioneer> List;
Trader trader;
vector<Bid> list;
public:
Auctioneer(){};
void accept_bids(const BidList& bid);
};
typedef vector<Auctioneer*> bidlist;
void Auctioneer::accept_bids(const BidList& bid){
BidList list;
//copy (BidList.begin(),BidList.end(),list);
}
//all the happy display commands
void show(const Bid &bid) {
cout << "\tBid\t(" << setw(3) << bid.bidId << "\t " << setw(3) << bid.trdId << "\t "
<< setw(3) << bid.type <<"\t " << setw(3) << bid.qty <<"\t " << setw(3) << bid.price <<")\t\n " ;
}
void show(const BidList &list) {
cout << "\t\tBidID | TradID | Type | Qty | Price \n\n";
for(BidList::const_iterator itr=list.begin(); itr != list.end(); ++itr) {
//cout <<"\t\t";
show(*itr);
cout << endl;
}
cout << endl;
}
//search now checks for failure
void show(const char *msg, const BidList &list) {
cout << msg << endl;
show(list);
}
void searchTest(BidList &list, int bidId) {
cout << "Searching for Bid " << bidId << endl;
BidList::const_iterator itr = find(list.begin(), list.end(), bidId);
if (itr==list.end()) {
cout << "Bid not found.";
} else {
cout << "Bid has been found. Its : ";
show(*itr);
}
cout << endl;
}
//comparator function for price: returns true when x belongs before y
bool compareBidList(Bid one, Bid two) {
if (one.type == 'A' && two.type == 'B')
return (one.price < two.price);
return false;
}
void sort(BidList &bidlist) { sort(bidlist.begin(), bidlist.end(), compareBidList); }
int main(int argc, char **argv) {
Trader trader;
BidList bidlist;
Auctioneer auctioneer;
//bidlist list;
auctioneer.accept_bids(bidlist);
//trader.loadRange(bidlist, NUMBIDS);
show("Bids before sort:", bidlist);
sort(bidlist);
show("Bids after sort:", bidlist);
system("pause");
return 0;
}