Vector container

I would like to put the following data into a vector container and start manuplating it.
but seems like am mixing up things.

expected output after inserting the objects in the vector
1 10 20 30 A 15
2 10 20 30 A 15
3 10 20 30 A 15
4 10 20 30 A 15
5 10 20 30 A 15
6 10 20 30 B 15
7 10 20 30 B 15
8 10 20 30 B 15
9 10 20 30 B 15



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <vector>
using namespace std;

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 Bid {
  int oper;
  int bidId; 
  int TrdId; 
  char Type; 
  int qty; 
  int price;
public:
  Bid() {0,0,0,'',0;}
  //Bid(int x,int y,int z,char m,int n) { d = x; }
  Bid &operator =(int x)
  {
      oper = x; return *this;
  }
  int buyerBids(int,int,int,char,int );
  int sellerBids(int,int,int,char,int ); 
};
int Bid::buyerBids(int a, int b,int c, char d, int e)
{
       bidId = a; 
       TrdId = b; 
       Type = c; 
       qty = d; 
       price = e;
       
     a=(rand() % 9) + 1; 
     b=(rand() % 9) + 1;
     c= (rand() % 30) + 1; ; 
     d='B';
     e=(rand() % 50) + 50;

     return (a,b,c,d,e);
}
int Bid::sellerBids(int a, int b,int c, char d, int e)
{
       bidId = a; 
       TrdId = b; 
       Type = c; 
       qty = d; 
       price = e;
       
     a=(rand() % 9) + 1; 
     b=(rand() % 9) + 1;
     c= (rand() % 30) + 1; ; 
     d='A';
     e=(rand() % 50) + 50;

     return (a,b,c,d,e);
}
//-----------------------------------------------------------------

int main()
{
  vector<Bid> v;

  int i;
  char A,B;

  cout << endl;
  cout << endl;
  cout << "Bid data successfully generated!\n\n"; 
  cout << "\n--------------Displaying your bid data---------------\n"; 
  cout << endl; 
 
 // displaying the stored data 
 cout << "\tBidID | TradID | Type  | Qty  |  Price  \n\n"; 
  for (i = 0; i <v.size(); i++)
   {
     v.push_back(v.[i].buyerBids(1,2,B,3));
     v.push_back(v[ i ].sellerBids(0,0,0,A,0));
   }
   cout << endl;
   system("pause");
   return 0;
}
v.push_back(v.[i].buyerBids(1,2,B,3));
Whoops. That's... a problem.

C++ will not let you compile this code, as you have an extra decimal point. Even when you fix that, the code will still not compile, because you have a type conflict. Your vector takes Bids; you're trying to shove ints down it's push_back throat. Metaphorically, anyway.

Also, even if this code would compile and your buyerBids and sellerBids returned Bids, it would return a Segmentation Fault. That vector is completely empty upon initialization, so you can't call an inexistent element from inside it to push_back() the vector.

http://cplusplus.com/reference/stl/vector/

-Albatross

Well, I did realise that it was messy! am just not that good at c++.But after a few hours I have brought the program to some life.I need some tweking to make it better.

I want to assign the pushed back data automatically. that is what I have come to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

// This class stores information on parts.
class Bid {
    int bidId; 
    int trdId; 
    int type; 
    int qty; 
    int price; 
    
public:

    Bid() { bidId = 0; trdId = 0; type = 0; qty = 0; price = 0; }// Default constructor.
    Bid(int n, int m, int o, int p ,int q) {  // Construct a complete Bid object.
        bidId = n; 
        trdId =m; 
        type =o; 
        qty =p; 
        price =q;
    }
    ~Bid(){}
    
    
int set_bidId(int a) { a = (rand() % 9) + 1;}
int set_trdId(int b) { b = (rand() % 9) + 1;}
int set_type(int c)  { c = (rand() % 9) + 1;}
int set_qty(int d)   { d = (rand() % 9) + 1;}
int set_price(int e) { e = (rand() % 50) + 50;}

// Accessor functions for Bid data.
int get_bidId() { return bidId; }
int get_trdId() { return trdId; }
int get_type()  { return type; }
int get_qty()   { return qty; }
int get_price() { return price; }
};

//---------------------Show bid details ----------------------------------------
void show(const char *msg, vector<Bid> vect);

bool operator<(Bid a, Bid b)
    {
    return a.get_bidId() < b.get_bidId();
    }

bool operator==(Bid a, Bid b)
    {
    return a.get_bidId() == b.get_bidId();
    }

int main()
{
vector<Bid> Bidlist;
// Initialize the Bids list.

    Bidlist.push_back(Bid(1,2,3,12,60));
    Bidlist.push_back(Bid(2,7,4,20,89));
    Bidlist.push_back(Bid(3,2,8,30,90));
    Bidlist.push_back(Bid(4,2,9,40,40));

// Display contents of the vector.
show("Bids that have been sorted:\n", Bidlist); // Use the sort() algorithm to sort the Bids list.
cout << endl;                                    // This requires that operator<() be defined for Bid.

sort(Bidlist.begin(), Bidlist.end());

show("Bids that have been matched:\n", Bidlist); // Use the find() algorithm to find a Bid given its number.
                                                  // This requires that operator==() be defined for Bid.

cout<< endl;
cout << "Searching for a particular Bid\n";
vector<Bid>::iterator itr;
itr = find(Bidlist.begin(), Bidlist.end(), Bid(4,2,9,40,40));
cout << "Bid has been found. Its : " << itr->get_bidId() << ".\n";

system("pause");
return 0;
}


// Display the contents of a vector<Bid>.
void show(const char *msg, vector<Bid> vect) {
vector<Bid>::iterator itr;
    cout << msg;
    cout <<"\t\tBidID | TradID | Type  | Qty  |  Price  \n\n"; 
    for(itr=vect.begin(); itr != vect.end(); ++itr)
    {
        cout <<"\t\t " << itr->get_bidId()<< "\t "<< itr->get_trdId() << "\t " << itr->get_type()<< "\t " 
        << itr->get_qty()<< "\t " << itr->get_price()<< "\n";
        cout << "\n";
    }
}
Topic archived. No new replies allowed.