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?

Never mind! I solved that problem by simply adding bidID = 0; into void Trader :: buy() and void Trader :: sell()

Now I've one more problem - so what Trader.h do is to generate randomly number of type, quantity and price, eg that program runs and display below:

[BidID, TraderID, Type, Quantity, Price]
(0, 0, B, 12, 67)
(1, 0, B, 5, 0)
(2, 0, B, 30, 24)
(3, 0, B, 19, 58)
(4, 0, B, 23, 64)
(5, 0, B, 6, 45)
(6, 0, B, 2, 27)
(7, 0, B, 2, 91)
(8, 0, B, 26, 42)
(9, 0, B, 28, 36)
(10, 1, A, 22, 4)
(11, 1, A, 3, 53)
(12, 1, A, 23, 82)
(13, 1, A, 22, 16)
(14, 1, A, 9, 95)
(15, 1, A, 18, 26)
(16, 1, A, 12, 38)
(17, 1, A, 10, 12)
(18, 1, A, 18, 99)
(19, 1, A, 26, 94)

so the Auctioneer receives that output - I want Auctioneer to compare two bids can be matched if the following conditions are satisfied:
(1). One is an ask bid and the other is a buy bid;
(2). The asking price is no higher than the buying price.

An example of output should display like that below:

Matched bids:
(0, 0, A, 13, 89) (11, 1, B, 29, 92)
(2, 0, A, 4, 61) (10, 1, B, 17, 82)
(6, 0, A, 13, 52) (10, 1, B, 13, 82)
(9, 0, A, 2, 106) (14, 1, B, 13, 118)
(1, 0, A, 14, 84) (11, 1, B, 16, 92)
(7, 0, A, 20, 52) (11, 1, B, 2, 92)
(3, 0, A, 14, 109) (14, 1, B, 11, 118
(7, 0, A, 18, 52) (12, 1, B, 20, 88)
(3, 0, A, 3, 109) (16, 1, B, 25, 117)

How do I do that? :confused:

Never mind! I solved that problem by simply adding bidID = 0; into void Trader :: buy() and void Trader :: sell()

Now I've one more problem - so what Trader.h do is to generate randomly number of type, quantity and price, eg that program runs and display below:

[BidID, TraderID, Type, Quantity, Price]
(0, 0, B, 12, 67)
(1, 0, B, 5, 0)
(2, 0, B, 30, 24)
(3, 0, B, 19, 58)
(4, 0, B, 23, 64)
(5, 0, B, 6, 45)
(6, 0, B, 2, 27)
(7, 0, B, 2, 91)
(8, 0, B, 26, 42)
(9, 0, B, 28, 36)
(10, 1, A, 22, 4)
(11, 1, A, 3, 53)
(12, 1, A, 23, 82)
(13, 1, A, 22, 16)
(14, 1, A, 9, 95)
(15, 1, A, 18, 26)
(16, 1, A, 12, 38)
(17, 1, A, 10, 12)
(18, 1, A, 18, 99)
(19, 1, A, 26, 94)

so the Auctioneer receives that output - I want Auctioneer to compare two bids can be matched if the following conditions are satisfied:
(1). One is an ask bid and the other is a buy bid;
(2). The asking price is no higher than the buying price.

An example of output should display like that below:

Matched bids:
(0, 0, A, 13, 89) (11, 1, B, 29, 92)
(2, 0, A, 4, 61) (10, 1, B, 17, 82)
(6, 0, A, 13, 52) (10, 1, B, 13, 82)
(9, 0, A, 2, 106) (14, 1, B, 13, 118)
(1, 0, A, 14, 84) (11, 1, B, 16, 92)
(7, 0, A, 20, 52) (11, 1, B, 2, 92)
(3, 0, A, 14, 109) (14, 1, B, 11, 118
(7, 0, A, 18, 52) (12, 1, B, 20, 88)
(3, 0, A, 3, 109) (16, 1, B, 25, 117)

How do I do that? :confused:

Great, no one replies back, but whatever. I hope someone reply back to this comment!

Referring to my comment, I added the function into Auctioneer.h so it should compare the bids between type A and type B (description is in my previous comment) but they output is very strangely random. The code is here:

class Auctioneer {
      string message1;
      string message2;

      public:
             int receiveBuy(string bb) {message1 = bb;}
             int receiveSell(string ss) {message2 = ss;}
             void getBids();
             void matchBids();
             Trader array[];
             
};

void Auctioneer :: getBids() {
     cout << message1 << endl;
     cout << message2 << endl;
};

void Auctioneer :: matchBids() {
     for (int x = 0; x < 20; x++) {
         for (int i = 0; i < 20; i++) {
             if (array[x].type!=array[i].type) {
                if (array[x].price>=array[i].price) {
                   cout << "(" << array[x].bidID << array[x].traderID << array[x].type <<
                   array[x].quantity << ", " << array[x].price << ")" << endl;
                   cout << "(" << array[i].bidID << array[i].traderID << array[i].type <<
                   array[i].quantity << ", " << array[i].price << ")" << endl << endl;
                   }
                }
             }
         }
     }

Any suggestions?

Which compiler are you using? Isn't it giving any warnings?

The array is a zero-sized array, you want to specify a reasonable size (a compile time constant) for it, in order to use it. Alternatively, perhaps use std::vector<Trader> .

Which compiler are you using? Isn't it giving any warnings?

The array is a zero-sized array, you want to specify a reasonable size (a compile time constant) for it, in order to use it. Alternatively, perhaps use std::vector<Trader> .

I am using Dev-C++ 4.9.9.2 and no it doesn't give any warning or error message at all. They ran successfully but the output is strange and long, like that:

(7680═3040, 66)

(00 4198944, 2147319808)
(0944791496\94, 4028)

(00 4198944, 2147319808)
(-13227772764900V4988, 92

(00 4198944, 2147319808)
(2362☺380, 5584)
....and so on.

I don't know what std::vector<Trader> is. Can you explain? What function & how does it work?

I already put the specific size of array shown below:

for (int x = 0; x < 20; x++) {
for (int i = 0; i < 20; i++) {

I need the Auctioneer to receive the variables like type, quantity and price from Trader so Auctioneer can compare bids.

I tried to put message1[x].type, message1[x].quantity, message1[x].price and message2.type, message1.quantity, message1.price

since message1 and message2 receives the bids from Trader like Auctioneer has message1 and message2 in getBids(); to display generate bids. (Sorry my English is not very good but i hope you understand what i am trying to say.)

Edited 6 Years Ago by boiishuvo: n/a

When you declare an array, you need to give the size of the array

int main()
{
    int arr[];
           
    cin.get();
	return 0;
}

Also how is the array getting its values. In post 1 the code you posted, the buy and sell bids were stored in objects in the functions. They get lost when the functions ends... You have to figure out a way of more permanent storage

I am using Dev-C++ 4.9.9.2 and no it doesn't give any warning or error message at all.

Dev-C++ is an IDE, not a compiler. You are probably using g++ to compile your code.
Find a way to pass the -Wall option to your compiler, it enables most of the warnings. To see an outline of g++ warning options that you can use, do g++.exe --help=warnings on the command line.

They ran successfully but the output is strange and long ...

Yes, strange because you operated through a zero-sized array.

I don't know what std::vector<Trader> is. Can you explain? What function & how does it work?

std::vector would be your dynamic array of Trade objects. A vector grows (and optionally shrinks) dynamically as needed - a handy feature.
See std::vector.

I already put the specific size of array shown below:

No, the array needs to be declared with a compile-time-constant size.
I meant something along the lines of this ..

struct Trade
{
  ...
};

struct Auctioneer
{
  static const int _traders_max = 123;

  // 123 Trade objects allocated for every instance of
  // class Auctioneer
  Trade array[_traders_max];
};

When you declare an array, you need to give the size of the array

int main()
{
    int arr[];
           
    cin.get();
	return 0;
}

Also how is the array getting its values. In post 1 the code you posted, the buy and sell bids were stored in objects in the functions. They get lost when the functions ends... You have to figure out a way of more permanent storage

permanent storage? it sounds complicated. I built these files with communication between classes - I added a function in the auctioneer that allows it to receive the buy() and sell() then display them and it worked so I just need the auctioneer to receive three specifics variables - type quantity and price like i mentioned before then they should match the bids as i already added the function in the auctioneer.

as you mentioned

int main()
{
    int arr[];
           
    cin.get();
	return 0;
}

where am i supposed to put that? sorry if i offend u or anything like that but i've been working on the same problem for two days and it made me having a headache.

It is printing because you are printing the values within the function. Your code is similar to some thing of this sort

void func()
{
    int x=5;
    cout<<x; 
}

int main()
{
    func();
}

This code will print the value of 5, but the value of 5 exists only within func. If you want it outside the function as well you do something of this sort

void func(int *x)
{
    *x=5;    
}

int main()
{
    int x; 
    func(&x);
    cout<<x; 
}

Similarly in you code pass the array of structs(objects) as an argument to the Trader::buy and Trader::sell functions.
Then when you want to match the bids fuction, sends these 2 arrays of objects as arguments into the function

where am i supposed to put that?

If you make an array, are you not supposed to put the size of the array in it as well ?
int arr[10];

It is printing because you are printing the values within the function. Your code is similar to some thing of this sort

void func()
{
    int x=5;
    cout<<x; 
}

int main()
{
    func();
}

This code will print the value of 5, but the value of 5 exists only within func. If you want it outside the function as well you do something of this sort

void func(int *x)
{
    *x=5;    
}

int main()
{
    int x; 
    func(&x);
    cout<<x; 
}

Similarly in you code pass the array of structs(objects) as an argument to the Trader::buy and Trader::sell functions.
Then when you want to match the bids fuction, sends these 2 arrays of objects as arguments into the function


If you make an array, are you not supposed to put the size of the array in it as well ?
int arr[10];

Trader.h file

void Trader :: buy(double *BUY) {
     bidID = 0;
     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;
}

Auctioneer.h file

void Auctioneer :: matchBids() {
     double BUY;
     buy(&BUY);
     for (int x = 0; x < 20; x++) {
         for (int i = 0; i < 20; i++) {
             if (array[x].type!=array[i].type) {
                if (array[x].price>=array[i].price) {
                   cout << "(" << array[x].bidID << array[x].traderID << array[x].type <<
                   array[x].quantity << ", " << array[x].price << ")" << endl;
                   cout << "(" << array[i].bidID << array[i].traderID << array[i].type <<
                   array[i].quantity << ", " << array[i].price << ")" << endl << endl;
                }
             }
         }
     }
};

It has errors. What you posted an example of code from your previous comment, I don't know how to change and correct it for the class function.

Edited 6 Years Ago by boiishuvo: n/a

Dev-C++ is an IDE, not a compiler. You are probably using g++ to compile your code.
Find a way to pass the -Wall option to your compiler, it enables most of the warnings. To see an outline of g++ warning options that you can use, do g++.exe --help=warnings on the command line.

Sorry, I've never dealt with this. I don't know what g++ is but I don't think I use it to compile my code.

Okay I looked up the vector website. It sounds like it is useful but again I don't know how to put vector into my current code - class function. Like

template < class T, void Trader :: buy() {
/*class Allocator = allocator<T> > class vector; ???*/
     bidID = 0;
     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;
}

Edited 6 Years Ago by boiishuvo: n/a

In the buy function dont declare the array of bids inside the function.Declare it in the run function and pass it to the buy function. Similarly for the sell function.
Now pass these 2 arrays to the match bids function

In the buy function dont declare the array of bids inside the function.Declare it in the run function and pass it to the buy function. Similarly for the sell function.
Now pass these 2 arrays to the match bids function

what do you mean by run function? which one is that?

void Trader :: buy() {
     ...
             {
                     double *BUY;
                     traderID=0;
                     type = 'B';
                     bids[x].quantity = ((rand() % MaxQuantity) + MinQuantity); 
                     bids[x].price = ((rand() % MaxPrice - MinPrice) + MinPrice);
                     };
 ...

is that what you mean in the red highlight? sorry my english is not very good.

I mean the simulator::run function ....
Do you know the difference between pass by value and pass by reference ?

Edited 6 Years Ago by abhimanipal: n/a

This article has been dead for over six months. Start a new discussion instead.