Write c++ program: create a stock class

Hello, having some trouble with this program. The array in the main function won't work as I keep getting an error.

Question:

An Equity is a stock or share of a company that is traded in Equity Markets such as the NY Stock Exchange.
Typically, the stocks of a company is assigned a symbol (e.g. Apple Inc.'s stocks have the Symbol AAPL).
When someone buys stocks, they buy certain number of stocks (called volume) at a certain price as dictated by the
stock market. Everyday, when the stock market is open, stocks start at an opening price and at the end of the day, the
stocks have a closing price.


Design a Stock class that has data members for the following:

- Symbol
- Volume
- Opening Price
- Closing Price
- Buy Price

The class should be appropriately designed and be properly encapsulated.
- It should have meaningful names and accessibility for the data members
- It should have a well designed Constructor(s)
- It should have appropriate accessors for all data members
- It should have appropriate mutators for all data members

- It should have an appropriate member function that computes current holding (H)
- H = volume * buy price

- It should have an appropriate member function that computes holding at market open (OH)
- HMO = volume * opening price

- It should have an appropriate member function that computes holding at market close (CH)
- HMC = volume * closing price


In your main program:

1. Create an array of 5 Stock objects.
You can pick the top 5 stocks from here:
https://finance.yahoo.com/most-active
you can find out some sample opening price by clicking on a symbol:
https://finance.yahoo.com/quote/AMD?p=AMD
Otherwise, you can always make up your own fictitious dummy data.
2. In a loop ask the user to enter the data for 5 stock objects in your array:
- Symbol, Volume, Buy Price, Opening Price, and Close Price.
- Use the mutators of your class to set the data

3. In another loop call the appropriate object methods to compute H, HMO, and HMC for the 5 stocks in your stock array,
and display the results. The Display can be like this (assuming user entered these symbols):

SYM=AAPL, H="amount", HMO="amount", HMC="amount"
SYM=IBM, H="amount", HMO="amount", HMC="amount"
SYM=CFO, H="amount", HMO="amount", HMC="amount"
SYM=TOY, H="amount", HMO="amount", HMC="amount"
SYM=TAR, H="amount", HMO="amount", HMC="amount"

("amount" should be replaced with the actual dollar figure, without the double quotes)

4. Finally show the Total Holding of all stocks in the portfolio (i.e your collection of 5 stocks), along with the Total Holding
at market open and the Total holding at market close. The Display should be like this:

SYMS = AAPL, IBM, CFO, TOY, TAR
Total H: "amount"
Total HMO: "amount"
Total HMC: "amount"
("amount" should be replaced with the actual dollar figure, without the double quotes)

Error Validation:
The following should result in an error and prompt user to correct data:
- Symbol is not allowed to be blank and should not be more than 5 characters long
- volume must be greater than 0.
- all prices must be greater than 0.


Program:


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
#include <iostream>
#include <string>
#include <iomanip>
//#include "stock.h"
#ifndef STOCK_H
#define STOCK_H
#endif
 

using namespace std;

class stock {

private:
	string Symbol;
	int volume;
	double open_price;
	double close_price;
	double buy_price;

public:
	//operations: member functions
	stock();
	stock(string symbol, int volume, double open_price, double close_price, double buy_price);
	virtual ~stock();

	string getSymbol();
	int  getVolume();
	double getOpenPrice();
	double getClosePrice();
	double getBuyPrice();

	void setSymbol(string Symbol);
	void setVolume(int Volume);
	void setOpenPrice(double Oprice);
	void setClosePrice(double Cprice);
	void setBuyPrice(double Bprice);

	double calCurrentHolding();
	double calOpenHolding();
	double calCloseHolding();

};


stock::stock()
{
	setBuyPrice(0);
	setClosePrice(0);
	setOpenPrice(0);
	setSymbol("");
	setVolume(0);
}

stock::stock(string symbol, int volume, double open_price, double close_price, double buy_price)
{
	setBuyPrice(buy_price);
	setClosePrice(close_price);
	setOpenPrice(open_price);
	setSymbol(symbol);
	setVolume(volume);
}

string stock::getSymbol()
{
	return Symbol;
}
int stock::getVolume()
{
	return volume;
}
double stock::getOpenPrice()
{
	return open_price;
}
double stock::getClosePrice()
{
	return close_price;
}
double stock::getBuyPrice()
{
	return buy_price;
}
void stock::setSymbol(string Symbol)
{
	Symbol = Symbol;
}
void stock::setVolume(int Volume)
{
	Volume = Volume;
}
void stock::setOpenPrice(double Oprice)
{
	open_price = Oprice;
}
void stock::setClosePrice(double Cprice)
{
	close_price = Cprice;
}
void stock::setBuyPrice(double Bprice)
{
	buy_price = Bprice;
}

double stock::calCurrentHolding()
{
	double h;
	h = (double)getVolume() * getBuyPrice();
	return h;
}
double stock::calOpenHolding()
{
	double hmo;
	hmo = (double)getVolume() * getOpenPrice();
	return hmo;
}
double stock::calCloseHolding()
{
	double hmc;
	hmc = (double)getVolume() * getClosePrice();
	return hmc;
}

stock::~stock()
{

}

int main() {
	
	int stock_array = 5;
	stock[stock_array];

	string symbol;
	int volume;
	double open_price;
	double close_price;
	double buy_price;

	for (int i = 0; i < stock_array; i++)
	{
		cout << "Stock" << i + 1 << endl;
	symbol:
		cout << "enter symbol: ";
		cin >> symbol;

		if ((symbol.find_first_not_of(' ') == std::string::npos) || symbol.empty() || (symbol.length() > 5))
		{
			cout << "symbol should not be empty and size <= 5. Try again\n";
			goto symbol;
		}

	volume:
		cout << "enter volume: ";
		cin >> volume;
		if (volume <= 0)
		{
			cout << "volume should be >0. Try again\n";
			goto volume;
		}

	openp: 
		cout << "enter opening price: ";
		cin >> open_price;

		if (open_price <= 0)
		{
			cout << "All prices should be >0. Try again\n";
			goto openp;
		}

	closep:
		cout << "Enter closing price: ";
		cin >> close_price;

		if (open_price <= 0)
		{
			cout << "all prices should be >0. Try again\n";
			goto closep;
		}
		
	buyp:
		cout << "enter buying price: ";
		cin >> buy_price;

		if (open_price <= 0)
		{
			cout << "All prices should be >0. Try again\n";
			goto buyp;
		}

		stocks[i] = stock(symbol, volume, open_price, close_price, buy_price);
	}

	//cout << endl << std::setw(20) << "-----------Displaying Holdings Data ----------" << endl;
	for (int i = 0; i < stock_array; i++)
	{
		cout << "SYM = " << right << stocks[i].getSymbol() << ", H = " << stocks[i].calCurrentHolding() <<
			", HMO = "<< stocks[i].calOpenHolding()<<", HMC = "<< stocks[i].calCloseHolding()<< endl;
	}

	double totalh = 0;
	double totalhmo = 0;
	double totalhmc = 0;

	cout << endl << "SYMS =";
	for (int i = 0; i < stock_array; i++)
	{
		totalh = totalh + stocks[i].calCurrentHolding();
		totalhmo = totalhmo + stocks[i].calOpenHolding();
		totalhmc = totalhmc + stocks[i].calCloseHolding();
		cout << stocks[i].getSymbol() << ", ";
	}
	cout << endl;
	cout << "Total H: " << totalh << endl;
	cout << "Total HMO: " << totalhmo << endl;
	cout << "Total HMC: " << totalhmc << endl;
	
	system("pause");
	return 0;
}


Thank you!
Last edited on
https://stackoverflow.com/help/how-to-ask

This isn't sufficient:

Hello, having some trouble with this program.


Please use code tags:

http://www.cplusplus.com/articles/z13hAqkS/

Then we can compile it with cpp.sh, have line numbers, keywords etc.

Try to avoid goto, there is a better way, namely a loop.
Topic archived. No new replies allowed.