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 :

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#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
}




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.