Accessing a vector of class object function

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:

#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.
Sorry I cannot explain now but this is the code. You can compare and see you problems.

Code:

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
#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;
        tmpOrder.assignOrder(bPrice,bLots,bAcct);

        for (i=0; i <= orders.size(); i++)
        {
            orders.push_back(tmpOrder);
            tmpOrder.printOrder();
            cout << "\n";
            break;
        }

    }

    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<<endl;
}


Oh, and please use code tags as I've done, thenext time you are posting a code.
Topic archived. No new replies allowed.