Displaying the contents of a vector

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1511 Views - Last Post: 07 October 2012 - 02:51 PM Rate Topic: -----

#1 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Displaying the contents of a vector

Posted 27 September 2012 - 10:28 PM

Hi,

I am trying to display the contents of a vector.

The vector is defined from a class I created.

vector <myClass> myList;


My question is how do I find out what's in myList? I have tried the
cout<<
but the operands are not recognized. I even tried a for loop, however that is not the correct way for vectors with class objects.

Any help would be greatly appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying the contents of a vector

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Displaying the contents of a vector

Posted 27 September 2012 - 10:38 PM

Please post a complete program that illustrates your question.

But remember to access an element of a vector, the element must already exist. If elements have been inserted into the vector then you can use array notation to print the elements of the vector.
cout << myList[0].SomeMemberVariable;

That is if SomeMemberVariable is a public variable in the myClass class.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Displaying the contents of a vector

Posted 27 September 2012 - 10:43 PM

I guess I am still doing this wrong... 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";
}


This post has been edited by nquadr: 28 September 2012 - 02:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Displaying the contents of a vector

Posted 28 September 2012 - 02:28 AM

moved to c++
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5942
  • View blog
  • Posts: 12,870
  • Joined: 16-October 07

Re: Displaying the contents of a vector

Posted 28 September 2012 - 06:01 AM

Your showInfo is profoundly wrong. SalesOrder should store data. It should pring the data it's stored. You shouldn't pass anything to it, let alone by reference.

It's debateable if you want user interaction in you object; I'd say no.

You'd want something more like:
class SalesOrder {
public:
	SalesOrder(int, int, int);
	void showInfo() const;
	int getBidPrice() const;
	int getLots() const;
	int getAccountNumber() const;
private:
	int bidPrice, lots, accountNumber;
};


SalesOrder enterSalesOrder();
void showSales(const vector <SalesOrder> &);

int main() {
	vector <SalesOrder> sOList;
	cout<<"This is a simulated trading Program\n\n\n";
	
	bool done = false;
	while(!done) {
		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;
		switch (selection) {
			case 'B':
			case 'b':
				sOList.push_back(enterSalesOrder());
				showSales(sOList);
				break;

			case 'O':
			case 'o':
				cout << "Come back later" << endl;
				break;

			case 'Q':
			case 'q':
				done = true;
				break;

			default:
				cout << "Not a valid option" << endl;
		}
	}
	return 0;
}

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




Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Displaying the contents of a vector

Posted 28 September 2012 - 07:01 AM

You must also be careful when you name your parameters of your member functions. You should never name a parameter the same as a member variable. For example:

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

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

Notice that in your member function you have variables with the same names as your class variables. This will cause the class member variables to be "hidden" by these parameters. This because the parameter variables are different variables than the class member variables, even though they have the same names. And because of this you will not modify your class member variables because the program is using the variables you passed into the function. So when the function exits any changes to these variables in the function will be lost. There are ways to force using the class variables in this case, but in my opinion it is better not do this.

Also, in my opinion, when you declare a function you shouldn't remove the variable names. Instead make the function prototype look the same as the function implementation.

class SalesOrder
{
      int bidPrice;
      int lots;
      int accountNumber;
   public:
      void eSalesOrder(int bid, int lot, int account);
};

void SalesOrder::eSalesOrder(int bid, int lot, int account)
{
}
{


Notice how the parameter names differ from the class variables. Also as already noted by baavgai you probably shouldn't be passing parameters into this function at all because you want to use the member variables, not the parameters.

Next you should get into a habit of always explicitly specifying the public, private, and protected access areas instead of relying on the defaults.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Displaying the contents of a vector

Posted 30 September 2012 - 12:31 AM

I was working on the code again. Got the vector to display correctly. Now I am trying to compare bidPrice with the offerPrice, but I can't seem to assign it a value not sure what I am doing wrong. Here is the code below:

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

using namespace std;


//Headers

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

public:

	void eSalesOrder(int bid, int lot, int account);
	void setOfferPrice(int offer);
	int getOfferPrice();
	void showInfo() const;
	bool executable(int price);
	void execute();
	SalesOrder();
};

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

//Global definitions
SalesOrder salesOrder;
void showList(const vector <SalesOrder> &);
void checkList(vector <SalesOrder>);




int main(int argc, char* argv[])
{
	vector <SalesOrder> sOList;
	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.setOfferPrice(offer);
			checkList(sOList);			
			break;

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

		}//end switch
	}//end while



	return 0;
}//end main

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

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


void SalesOrder::showInfo() const
{
	cout << bidPrice <<"\n";
	cout << lots <<"\n";
	cout << accountNumber <<"\n";
	cout << executed <<"\n";
}//end fxn

void checkList(vector <SalesOrder> sOList)
{
	int price;
	for(unsigned int counter = 0; counter < sOList.size(); counter++)
		sOList[counter].execute();
}//end fxn

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

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

int SalesOrder::getOfferPrice() 
{
	return offerPrice;
}
//bool SalesOrder::executable(int price)
//{
//	offerPrice = price;
//
//}//end fxn


Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: Displaying the contents of a vector

Posted 30 September 2012 - 12:41 AM

You need to review you understanding of instances and how vector<> works. You are setting the offer price on your global instance of salesOrder, but you are printing out the values in your sOList.
Was This Post Helpful? 0
  • +
  • -

#9 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Displaying the contents of a vector

Posted 30 September 2012 - 12:43 AM

View PostSkydiver, on 30 September 2012 - 12:41 AM, said:

You need to review you understanding of instances and how vector<> works. You are setting the offer price on your global instance of salesOrder, but you are printing out the values in your sOList.


Does that mean I have to loop the value of offerprice to my list first?

What I am trying to do is compare my offerPrice and bidPrice.

This post has been edited by nquadr: 30 September 2012 - 01:01 AM

Was This Post Helpful? 0
  • +
  • -

#10 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Re: Displaying the contents of a vector

Posted 30 September 2012 - 03:35 AM

Do you want to set an offer price for each element in the vector, or do you just want to store 1 offer price to compare with the vector. If you want to store multiple offer price in the vector you should use .at().

 
case 'o':
case 'O':
	cout<<"Please enter the offer price: ";
	cin>> offer;
        //set offer price for the last element in the vector
	sOList.at(sOList.size() - 1).setOfferPrice(offer); 
	//salesOrder.setOfferPrice(offer);
	checkList(sOList);
	break;


This post has been edited by rethc: 30 September 2012 - 03:37 AM

Was This Post Helpful? 1
  • +
  • -

#11 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Displaying the contents of a vector

Posted 30 September 2012 - 08:42 PM

Actually no I just wanted one offerPrice. I believe I have solved that part. However, what I would like to solve now is to let the trade know it has been executed. I would like to do this without setting up a global counter variable, but not sure of the proper way to do this. I have commented the line where I needed help. Any help would be greatly appreciated. Rethc, I am trying to do what you suggested but only for the executed variable. 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();
	void setExecuted(bool isExecuted);
	SalesOrder();
};

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

//Global definitions
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;
			setOfferPrice(offer);
			checkList(sOList);			
			break;

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

		}//end switch
	}//end while



	return 0;
}//end main

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

void SalesOrder::eSalesOrder(int bid, int lot, int account)
{
	bidPrice = bid;
	lots = lot;
	accountNumber = account;
}//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)
{
	for(unsigned int counter = 0; counter < sOList.size(); counter++)
		sOList.at(counter).execute();
}//end fxn

void SalesOrder::setExecuted(bool isExecuted)
{
	executed = isExecuted;
}

void SalesOrder::execute()
{
		//offerPrice = getOfferPrice();
		
		if (offerPrice == bidPrice  && salesOrder.executed !=true)
		{
			sOList.at(counter).setExecuted(true);  //need something to this effect but cannot access the counter variable here, and don't want to define globally
			cout<< "The trade for "<<lots<<" lots with account number: "<<accountNumber
				<< " has been executed for "<<bidPrice<<"\n";
		}//end if
	
}//end fxn

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

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


Line 138 specifically.

This post has been edited by nquadr: 30 September 2012 - 08:43 PM

Was This Post Helpful? 0
  • +
  • -

#12 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Displaying the contents of a vector

Posted 06 October 2012 - 11:44 PM

I have fixed the above problem, but I am having logic problems with one of my functions. Specifically the checkList function. I want to compare prices to execute a trade...but my logic is flawed. I think it pertains to the line I have commented. Any help would be greatly appreciated.


#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  && salesOrder.executed !=true)
		{
			executed = true;
		}//end if
	return executed > 0;
}//end fxn

bool SalesOrder::getExecuted()
{
	return executed;
}//end fxn


Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: Displaying the contents of a vector

Posted 07 October 2012 - 04:42 AM

Your use of global variables, in particular salesOrder makes your code very confusing. I suggest redesigning so that you don't need that global, and you pass in the appropriate values for comparison instead.
Was This Post Helpful? 0
  • +
  • -

#14 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Displaying the contents of a vector

Posted 07 October 2012 - 01:21 PM

View PostSkydiver, on 07 October 2012 - 04:42 AM, said:

Your use of global variables, in particular salesOrder makes your code very confusing. I suggest redesigning so that you don't need that global, and you pass in the appropriate values for comparison instead.



Unfortunately, the deadline for the project is tomorrow. I would not be able to re-design it. Any suggestions on how to make the checkList function function as it is?
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Displaying the contents of a vector

Posted 07 October 2012 - 01:51 PM

If you refuse to take the advise and stop using the global variables you must stop passing these global variables into your functions. When you pass a variable into your function that has the same name as your global variable you will use the variable you pass as a parameter, not the global. Even though they have the same name they are not the same variable.

I suggest you take the advise given and stop using the global variables, and learn to pass the variables correctly.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2