Hi,

I am trying to display the contents of a vector.

The vector is defined from a class I created. I seem to be only storing the most recent values in my vector. I am not sure where my logic is incorrect. I am trying to store multiple objects in my vector. Any help would be greatly appreciated. Code is below:

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;


class SalesOrder
{
       int bidPrice, lots, accountNumber;
public:
       void eSalesOrder(int, int, int);
       bool executable(int);
       void execute();
       void showInfo(int&, int&, int&) const;
       SalesOrder();
};

SalesOrder::SalesOrder() //default constructor
{
       bidPrice = 0;
       lots = 0;
       accountNumber = 0;
}




int main(int argc, char* argv[])
{


        cout<<"This is a simulated trading Program\n\n\n"
                <<"Please choose from the following options\n\n"
                <<"Choose \'B\' to Bid\n"
                <<"Choose \'O'\ to change Offer Price\n"
                <<"Choose \'Q'\ to Quit\n\n";


              vector <SalesOrder> sOList;



              int bP, l, aN;
              unsigned int counter;
        char selection;
              selection = 'd';

              while(selection != 'Q')
              {

                     cin>>selection;
                     SalesOrder salesOrder;
                     switch (selection)
                     {
                           case 'b':
                                         cout << "Please enter the bid Price, lots, and the account number, \neach followed by a space: ";


                                         cin >> bP >> l >> aN;
                                         salesOrder.eSalesOrder(bP, l, aN);
                                         sOList.push_back(salesOrder);
                                         for(counter = 0; counter < sOList.size(); counter++)
                                         {
                                                sOList[counter].showInfo(bP, l, aN);
                                         }
                                         break;
                           case 'B':
                                         cout<<"B\n";
                                         break;
                           case 'q':
                                         cout<<"Q\n";
                                         selection = 'Q';
                                         break;
                           case 'Q':
                                         cout<<"Q\n";
                                         selection = 'Q';
                                         break;
                           case 'O':
                                         cout<<"O\n";
                                         break;
                           case 'o':
                                         cout<<"O\n";
                                         break;
                           case 'd':
                                         cout<<"Please make a selection\n";
                                         break;
                     }//end switch
                     cout<<"Please choose from the following options\n\n"
                <<"Choose \'B\' to Bid\n"
                <<"Choose \'O'\ to change Offer Price\n"
                <<"Choose \'Q'\ to Quit\n\n";
              }//end while




        system("pause");
        return 0;
}

void SalesOrder::eSalesOrder(int bidPrice, int lots, int accountNumber)
{

       bool reEnter = true;

       while(reEnter == true)
       {

              cout<<"You entered bid price: "<<bidPrice<<"\n";
              cout<<"You entered lots: "<<lots<<"\n";
              cout<<"You entered account number: "<<accountNumber<<"\n\n\n";

              if (lots % 100 > 0)
              {
                     cout<<"Invalid Lots amount\n\n";
                     cout << "Please enter the bid Price, lots, and the account number, \neach followed by a space: ";
                     cin >> bidPrice >> lots >> accountNumber;
              }//end if
              else
                     reEnter = false;


       }//end while
}//end eSalesOrder function


void SalesOrder::showInfo(int& bidPrice, int& lots, int& accountNumber) const
{
       cout << "The bid price is " << bidPrice << "\n";
       cout << "The lot size is " << lots << "\n";
       cout << "The account number is " << accountNumber << "\n\n";
}

There is nothing wrong with your Vector.. It's the for-loop logic.
for(counter = 0; counter < sOList.size(); counter++)

when that for-loop ends, counter will have retained its value from the last iteration.

you need:

for(unsigned int counter = 0; counter < sOList.size(); counter++)

That way, counter starts at 0 all the time then iterates the vector appropriately.

Line 47 should be: 'b' not 'd'.. selection = 'b';

Also might I suggest that when a case statement contains multiple statements, you might want to start using brackets. Reason being is because if it contains pointers or handles, it'll fall through (well happened to me a lot.. Compiler complained). It's just practice incase. Not required all the time. Also I'd suggest having a 'default: ' case.

instead of:

case 'b':
          ...
          ...
         break;

//You can do:

case 'b':
        {
            .......
        }
        break;

 default:
        break;

Edited 4 Years Ago by triumphost

In general, prefer to declare a variable closest to its point of use. Local variables should have no more visibility than absolutely required; define them when you need them, and let them go out of scope when you're done with them.

For instance,

for( unsigned int i = 0 ; i < sOList.size() ; ++i )
{
    // code for first loop
}

// ...

for( unsigned int i = 0 ; i < sOList.size() ; ++i )
{
    // code for another loop
}

If you have a current compiler, to iterate through a vector:
for( const SalesOrder& order : sOList ) order.showInfo() ;

And your showInfo() should be:

class SalesOrder
{
       int bidPrice, lots, accountNumber;
    public:
       // ... 
       void showInfo() const ; // you don't need any parameters
       // ...
};

void SalesOrder::showInfo() const
{
       cout << "The bid price is " << bidPrice << "\n" ; // member variable bidPrice
       cout << "The lot size is " << lots << "\n";
       cout << "The account number is " << accountNumber << "\n\n";
}

I have fixed most of my code, however I am having diffuculty in my checkList function. I have commmented the line that I believe is causing the problem. Any help or direction would be highly appreciated. Thanks.

#include "StdAfx.h"
#include <iostream>
#include <vector>

using namespace std;


//Headers

class SalesOrder
{
    int bidPrice, lots, accountNumber;
    bool executed;

public:

    void eSalesOrder(int bid, int lot, int account);

    void showInfo() const;
    bool executable(int price);
    void execute();
    bool getExecuted();
    SalesOrder();
};

SalesOrder::SalesOrder() //default constructor
{
    bidPrice = 0;
    lots = 0;
    accountNumber = 0;
    executed = false;
}

SalesOrder salesOrder;
int offerPrice;
void showList(const vector <SalesOrder> &);
void checkList(vector <SalesOrder>);

void setOfferPrice(int offer);

int getOfferPrice();
vector <SalesOrder> sOList;



int main(int argc, char* argv[])
{

    cout <<"This is a simulated trading Program\n\n\n";


    int bP, l, aN, offer;
    bool repeat = true;
    while(repeat)
    {
        cout<<"Please choose from the following options\n\n"
            <<"Choose \'B\' to Bid\n"
            <<"Choose \'O\' to change Offer Price\n"
            <<"Choose \'Q\' to Quit\n\n";

        char selection;
        cin>>selection;
        switch(selection)
        {
        case 'b':
        case 'B':
            cout<<"Please enter the bid price"
                <<", lots, and the account number,"
                <<"\neach followed by a space: ";
            cin>> bP >> l >> aN;

            salesOrder.eSalesOrder(bP, l, aN);
            sOList.push_back(salesOrder);
            showList(sOList);
            break;

        case 'o':
        case 'O':
            cout<<"Please enter the offer price: ";
            cin>> offer;
            //salesOrder.executable(offer);
            setOfferPrice(offer);
            checkList(sOList);          
            break;

        case 'Q':
        case 'q':
            repeat = false;
            break;

        }//end switch
    }//end while



    return 0;
}//end main

void SalesOrder::eSalesOrder(int bid, int lot, int account)
{
    bidPrice = bid;
    lots = lot;
    accountNumber = account;
}//end fxn

void showList(const vector <SalesOrder> &sOList)
{
    for(unsigned int counter = 0; counter < sOList.size(); counter++)
    {
        sOList[counter].showInfo();
    }//end for
}//end fxn

void SalesOrder::showInfo() const
{
    cout<< "The trade for "<<lots<<" lots with account number: "<<accountNumber
                << " has been entered for $"<<bidPrice<<"\n";
    cout << executed <<"\n";
}//end fxn

void checkList(vector <SalesOrder> sOList)
{
    bool isExecuted = salesOrder.getExecuted();// I think there is a problem with this line
                                               //I need it to access all the elements that have executed
    //int offerPrice = getOfferPrice();
    for(unsigned int counter = 0; counter < sOList.size(); counter++)
    {
        salesOrder.executable(offerPrice);
        if (isExecuted == true)
        {
            sOList.at(counter).execute();
        }//end if
    }//end for
}//end fxn

void SalesOrder::execute()
{
    cout<< "The trade for "<<lots<<" lots with account number: "<<accountNumber
                << " has been executed for $"<<bidPrice<<"\n";    
}//end fxn

void setOfferPrice(int price)
{
    offerPrice = price;
}//end fxn

int getOfferPrice() 
{
    return offerPrice;
}//end fxn

bool SalesOrder::executable(int offerPrice)
{
    if (offerPrice == bidPrice  && executed !=true)
        {
            executed = true;
        }//end if
    return executed > 0;
}//end fxn

bool SalesOrder::getExecuted()
{
    return executed;
}//end fxn
This article has been dead for over six months. Start a new discussion instead.