Need help pushingback a vector of class object

I am trying to implement a vector of a class that when certain inputs from the user are keyed in, elements are assigned via a member function while simultaneously "pushing back" or increasing the size of the vector of the object. This is my second course in c++ but still really a novice when it comes to classes and how to do certain things with them. I've searched the forum and reference sections and can't find anything that would help.

My current code compiles and runs but it iterates back to the "while" loop as I believe it exits the for loop but I can't quite figure out why. I'm sure the answer is simple but can't get my head around it.

To reiterate, I think my current problem is "pushing back" (increasing the size of the vector when I assign the object members user given values) the vector.

My code is as follows:

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Order
{
int bidPrice;
int lots;
int acctNum;
public:
Order (int bidPrice, int lots, int acctNum);
Order ();
void assignOrder (int , int, int);
void printOrder ();
//void assignPrice (int newPrice);
//void assignLots (int newLots);
//void assignAcct (int newAcct);
bool executable(int offerPrice);
void execute(void);
};

//Order Orders;

int main ()
{
vector <Order> orders;
//Order tmpOrder;
int bPrice;
int bLots;
int bAcct;
int i;
string logOff;
while (logOff != "Quit")
{
cout << "Welcome to the Financial Instrument Order System\n";
cout << "To exit the program at any time, please type Quit\n";

cout << "Please Enter Your Order\n";
cout << "Price	 Lots	 Account Number\n";
cout << "_____	 ____	 ______________\n";

cin >> bPrice >> bLots >> bAcct;

for (int i=0; i < orders.size(); i++)
{
orders.push_back(orders[i]);
orders[i].assignOrder(bPrice, bLots, bAcct);
orders[i].printOrder();
cout << "\n";
}

}

return 0;
}

void Order::assignOrder(int bPrice, int bLots, int bAcct)
{
bidPrice = bPrice;
lots = bLots;
acctNum = bAcct;
}

void Order::printOrder()
{
cout << bidPrice;
cout << lots;
cout << acctNum;
}


Please excuse my ignorance with this topic.
Last edited on
Please edit you post and insert code tags (the <> formatting button) around your code. Without code tags, I can't make meaningful reference to the lines of your program with problems.
closed account (o1vk4iN6)
orders.push_back(orders[i]);

Looks like you are trying to use an order before it even exists.

You could make it a constructor instead.

1
2
3
4
5
6
7
8
9

Order::Order( int bidPrice, etc... ) : bidPrice( bidPrice ), etc...
{
}

// ...

order.push_back( Order( bPrice, bLots, bAcct ) );
Topic archived. No new replies allowed.