Firstly I want to show you the code and it can run properly:
You may copy each file and paste them into a C++ software and run it please.
Trader.h file
const int NumSeller = 1;
const int NumBuyer = 1;
const int NumBids = 10;
const int MinQuantity = 1;
const int MaxQuantity = 30;
const int MinPrice =50;
const int MaxPrice = 100;
class Trader {
string message1;
string message2;
private:
int bidID;
int traderID;
char type;
int quantity;
int price;
public:
void buy();
void sell();
void storeBids();
string getMessage1() {return message1;}
string getMessage2() {return message2;}
};
void Trader :: buy() {
string bb;
struct Trader bids[NumBids + 1];
for(int x = 0; x < NumBids; x++)
{
traderID=0;
type = 'B';
bids[x].quantity = ((rand() % MaxQuantity) + MinQuantity);
bids[x].price = ((rand() % MaxPrice - MinPrice) + MinPrice);
};
for(int i = 0; i < NumBids; i++)
{
cout << "(" << bidID++ << ", " << traderID << ", " << type << ", " <<
bids[i].quantity << ", " << bids[i].price << ")" << endl;
};
message1 = bb;
};
void Trader :: sell() {
string ss;
struct Trader bids[NumBids + 1];
for(int x = 0; x < NumBids; x++)
{
traderID=1;
type = 'A';
bids[x].quantity = ((rand() % MaxQuantity) + MinQuantity);
bids[x].price = ((rand() % MaxPrice - MinPrice) + MinPrice);
};
for(int i = 0; i < NumBids ; i++)
{
cout << "(" << bidID++ << ", " << traderID << ", " << type << ", " <<
bids[i].quantity << ", " << bids[i].price << ")" << endl;
};
message2 = ss;
};
Auctioneer.h file
class Auctioneer {
string message1;
string message2;
private:
Trader trader;
public:
int receiveBuy(string bb) {message1 = bb;}
int receiveSell(string ss) {message2 = ss;}
void matchBids();
};
void Auctioneer :: matchBids() {
cout << message1 << endl;
cout << message2 << endl;
};
Simulator.h file
class Simulator {
Trader trader;
Auctioneer auctioneer;
public:
void run();
};
void Simulator :: run() {
trader.buy();
trader.sell();
auctioneer.receiveBuy(trader.getMessage1());
auctioneer.receiveSell(trader.getMessage2());
auctioneer.matchBids();
}
Main file
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <windows.h>
#include <map>
#include <ctime>
#include <cstdlib>
using namespace std;
#include "Trader.h"
#include "Auctioneer.h"
#include "Simulator.h"
int main() {
Simulator s;
s.run();
system("pause");
return 0;
}
So the problem is when they run, they output like that
(2293700, 0, B, 12, 67)
(2293701, 0, B, 5, 0)
(2293702, 0, B, 30, 24)
...
But I didn't want the list bidID that shows strangly random numbers. I wanted to display like that
(0, 0, B, 12, 67)
(1, 0, B, 5, 0)
(2, 0, B, 30, 24)
...
I looked through my codes many of times but no luck to fix that problem. Somewhere in the Trader.h file should be fixed. Any suggestions?