### sorting with vectors

Hello everyone i'm writing a program of a stock market where i read from a file and sort with symbols and percent gain/loss. I have completed sorting with symbols but having trouble establishing the percent gain loss. First i designed and implemented the stock object. call the class stockType. main components of a stock are the stock symbol, stock price and number of shares. Second we have to create a list of stock objects. call the class to implement a list of stock objects stockListType. To store the list of stocks, i declared a vector and called the component type of this vector stockType.
Because the company requires me to produce the list ordered by percent gain/loss, i need to sort the stock list by this component. However, i'm not to physically sort the list by component percent gain/loss; instead i should provide a logic ordering with respect to this component. To do so i added a data member, a vector to hold the indices of the stock list ordered by the component percent gain/loss and i called this array indexByGain. I am going to use the array indexByGain to print the list. The elements of the array indexByGain will tell which component of the stock list to print next. I have trouble going about how to implement the function sortStockGain(). i need assistance and this would be of great help as i don't know how to start. below is my entire code and the txt file. I have sort symbols working but dont know how to implement the sort by gain if one could assist me this would be of great help. By the way this is my first programming class.

Below is my entire code:

[#ifndef STOCKTYPE_H
#define STOCKTYPE_H

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

class stockType
{
public:
stockType(string symbol="", double openPrice=0.0, double closePrice=0.0, double highPrice=0.0,
double lowPrevPrice=0.0, double closePrevPrice=0.0, int shares=0);

friend istream& operator>>(istream& ins, stockType& stock);
friend ostream& operator<<(ostream& outs, const stockType& stock);

//sets the variables
void setStockInfo(string syms, double open, double close, double high,
double lowPrev, double closePrev, int no_of_shares);
//calculates the gain
void calculateGain(double closeP, double prevP);

bool operator==(const stockType& stock1) const;
bool operator!=(const stockType& stock1) const;
bool operator<=(const stockType& stock1) const;
bool operator<(const stockType& stock1) const;
bool operator>=(const stockType& stock1) const;
bool operator>(const stockType& stock1) const;

//member functions
string getSymbols()const {return symbols;}
double getOpenPrice()const {return open_price;}
double getClosePrice()const {return close_price;}
double getHighPrice()const {return high_price;}
double getLowPrevPrice()const {return low_prev_price;}
double getClosePrevPrice()const {return close_prev_price;}
double getShares()const {return no_shares;}
double getGain()const {return gain;}

private:
string symbols;
double open_price, close_price, high_price, low_prev_price, close_prev_price, gain;
int no_shares;
};

#include "stockType.h"

stockType::stockType(string symbol, double openPrice, double closePrice, double highPrice,
double lowPrevPrice, double closePrevPrice, int shares)
{
setStockInfo(symbol, openPrice, closePrice, highPrice, lowPrevPrice, closePrevPrice, shares);
}

void stockType::setStockInfo(string syms, double open, double close, double high,
double lowPrev, double closePrev, int no_of_shares)
{
symbols = syms;
open_price = open;
close_price = close;
high_price = high;
low_prev_price = lowPrev;
close_prev_price = closePrev;
no_shares = no_of_shares;
}

istream& operator>>(istream& ins, stockType& stock)
{
ins>>stock.symbols;
ins>>stock.open_price;
ins>>stock.close_price;
ins>>stock.high_price;
ins>>stock.low_prev_price;
ins>>stock.close_prev_price;
ins>>stock.no_shares;
stock.calculateGain(stock.close_price, stock.close_prev_price);
return ins;

}

ostream& operator<<(ostream& outs, const stockType& stock)
{

outs<<stock.getSymbols()
<<fixed<<showpoint<<setprecision(2)
<<setw(10)<<stock.getOpenPrice()<<setw(10)
<<stock.getClosePrice()<<setw(10)
<<stock.getHighPrice()<<setw(10)
<<stock.getLowPrevPrice()<<setw(11)
<<stock.getClosePrevPrice()
<<setw(10)<<stock.getGain()<<"%"<<setw(13)
<<stock.getShares()<<endl<<endl;
return outs;
}

void stockType::calculateGain(double closeP, double prevP)
{

gain = ((closeP - prevP)/(prevP)*100);
}
bool stockType::operator==(const stockType& stock1) const
{
return (symbols==stock1.symbols);
}
bool stockType::operator!=(const stockType& stock1) const
{
return (symbols!=stock1.symbols);
}
bool stockType::operator>=(const stockType& stock1) const
{
return (symbols>=stock1.symbols);
}
bool stockType::operator>(const stockType& stock1) const
{
return (symbols>stock1.symbols);
}
bool stockType::operator<=(const stockType& stock1) const
{
return (symbols<=stock1.symbols);
}
bool stockType::operator<(const stockType& stock1) const
{
return (symbols<stock1.symbols);
}

#ifndef stockListType_H
#define stockListType_H

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "stockType.h"

using namespace std;

class stockListType
{
public:
stockListType()
{
}

void insert(const stockType& item);
void sortStockSymbols();
void sortStockGain();
void printStockSymbols();
void printStockGain();

private:
vector<int> indexByGain;
vector<stockType> list;
};
#endif

#include <algorithm>
#include "stockListType.h"

void stockListType::insert(const stockType& item)
{
list.push_back(item);
}

void stockListType::printStockSymbols()
{
int k = 0;
double closing_assets = 0;
cout<<"***************** First Investor's Heaven ******************************"<<endl<<endl;
cout<<" Financial Report "<<endl;
cout<<"********************************************************************************"<<endl<<endl;
cout<<"Stock Today Previous Percent"<<endl<<endl;
cout<<"Symbol Open Close High Low Close Gain Volume"<<endl;
cout<<"------ ------ ------ ------ ------- ------- ------- -------"<<endl;

for (k=0; k<list.size(); k++)
{
cout<<list[k]<<endl;
closing_assets =+(list[k].getClosePrice()*list[k].getShares());
}
cout<<endl<<"****************************************";
cout<<endl<<"* Closing Assests: "<<closing_assets<<endl;
cout<<"****************************************"<<endl;
}

void stockListType::printStockGain()
{
int k = 0;
double closing_assets =0;
cout<<"***************** First Investor's Heaven ******************************"<<endl<<endl;
cout<<" Financial Report "<<endl;
cout<<"********************************************************************************"<<endl<<endl;
cout<<"Stock Today Previous Percent"<<endl<<endl;
cout<<"Symbol Open Close High Low Close Gain Volume"<<endl;
cout<<"------ ------ ------ ------ ------- ------- ------- -------"<<endl;

for (k=0; k<list.size(); k++)
{
cout<<list[k]<<endl;
closing_assets = closing_assets + (list[k].getClosePrice()*list[k].getShares());
}
cout<<endl<<"****************************************";
cout<<endl<<"* Closing Assests: "<<closing_assets<<endl;
cout<<"****************************************"<<endl;

}

void stockListType::sortStockSymbols()
{
sort(list.begin(), list.end());

}

void stockListType::sortStockGain()
{
//need help implementing the code
}

][/code]

Have you seen std::sort?

With this you provide a pointer or iterator to a start point and an endpoint. It will then use the < operator to determine the order. If you'd like to use a more complicated formula, then you are also able to provide your own function or functor to determine the order.

Sorry, but it's hard to read your code. I see you attempted to put the code-tags, but I think you didn't finish the first one.
yes i think i have to use a pointer or iterator. could you please show me an example that would really help as it will act as a guideline. I am sorry this is my first programming class and i am new to this forum as i just joined today.
change
[#ifndef STOCKTYPE_H

to [.code]#ifndef STOCKTYPE_H (without the .)by using the edit key, it will make your code easier to read :

example :

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221`` ``````#ifndef STOCKTYPE_H #define STOCKTYPE_H #include #include #include using namespace std; class stockType { public: stockType(string symbol="", double openPrice=0.0, double closePrice=0.0, double highPrice=0.0, double lowPrevPrice=0.0, double closePrevPrice=0.0, int shares=0); friend istream& operator>>(istream& ins, stockType& stock); friend ostream& operator<<(ostream& outs, const stockType& stock); //sets the variables void setStockInfo(string syms, double open, double close, double high, double lowPrev, double closePrev, int no_of_shares); //calculates the gain void calculateGain(double closeP, double prevP); bool operator==(const stockType& stock1) const; bool operator!=(const stockType& stock1) const; bool operator<=(const stockType& stock1) const; bool operator<(const stockType& stock1) const; bool operator>=(const stockType& stock1) const; bool operator>(const stockType& stock1) const; //member functions string getSymbols()const {return symbols;} double getOpenPrice()const {return open_price;} double getClosePrice()const {return close_price;} double getHighPrice()const {return high_price;} double getLowPrevPrice()const {return low_prev_price;} double getClosePrevPrice()const {return close_prev_price;} double getShares()const {return no_shares;} double getGain()const {return gain;} private: string symbols; double open_price, close_price, high_price, low_prev_price, close_prev_price, gain; int no_shares; }; #include "stockType.h" stockType::stockType(string symbol, double openPrice, double closePrice, double highPrice, double lowPrevPrice, double closePrevPrice, int shares) { setStockInfo(symbol, openPrice, closePrice, highPrice, lowPrevPrice, closePrevPrice, shares); } void stockType::setStockInfo(string syms, double open, double close, double high, double lowPrev, double closePrev, int no_of_shares) { symbols = syms; open_price = open; close_price = close; high_price = high; low_prev_price = lowPrev; close_prev_price = closePrev; no_shares = no_of_shares; } istream& operator>>(istream& ins, stockType& stock) { ins>>stock.symbols; ins>>stock.open_price; ins>>stock.close_price; ins>>stock.high_price; ins>>stock.low_prev_price; ins>>stock.close_prev_price; ins>>stock.no_shares; stock.calculateGain(stock.close_price, stock.close_prev_price); return ins; } ostream& operator<<(ostream& outs, const stockType& stock) { outs<=(const stockType& stock1) const { return (symbols>=stock1.symbols); } bool stockType::operator>(const stockType& stock1) const { return (symbols>stock1.symbols); } bool stockType::operator<=(const stockType& stock1) const { return (symbols<=stock1.symbols); } bool stockType::operator<(const stockType& stock1) const { return (symbols #include #include #include #include "stockType.h" using namespace std; class stockListType { public: stockListType() { } void insert(const stockType& item); void sortStockSymbols(); void sortStockGain(); void printStockSymbols(); void printStockGain(); private: vector indexByGain; vector list; }; #endif #include #include "stockListType.h" void stockListType::insert(const stockType& item) { list.push_back(item); } void stockListType::printStockSymbols() { int k = 0; double closing_assets = 0; cout<<"***************** First Investor's Heaven ******************************"<

Last edited on
Stewbond wrote:
Have you seen std::sort?
eni001 wrote:
yes i think i have to use a pointer or iterator. could you please show me an example that would really help as it will act as a guideline.
There is an example with std::vector here: http://www.cplusplus.com/reference/algorithm/sort/
Topic archived. No new replies allowed.