outputting values from class

Hello, in this program, when I output total_val I get 0, when it should be 500 and 21. I can get the program to work if I write an external function but not when I put the function definition within the class.

This is the output:
Stock holdings:
Company: Apple
Numshares: 10
Price: 50
Total price: 0
Company: Micro
Numshares: 7
Price: 3
Total price: 0

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
  #include <iostream>		
#include <string>		
using namespace std;
class Stock
{
private:
	string company;
	int numshares;
	double price;
public:
	Stock(const string & co, long n = 0, double p = 0.0);
	void show();
	double total_val;
	void set_tot() { total_val = numshares * price; }
};
const int numarr = 2;
int main()
{
	Stock stocks[numarr] = { Stock("Apple", 10, 50.00),
		Stock("Micro", 7, 3.00) };
	cout << "Stock holdings:\n";
	stocks[0].show();
	stocks[1].show();
	return 0;
}
Stock::Stock(const string & co, long n, double p)
{
	company = co;
	numshares = n;
	price = p;
	total_val = 0.0;
}
void Stock::show()
{
	cout << "Company: " << company << endl;
	cout << "Numshares: " << numshares << endl;
	cout << "Price: " << price << endl;
	cout << "Total price: " << total_val << endl;
}
You assigned it zero. Perhaps you should assign it the value of p* n.
total_val = 0.0;

This is the code that sets total_val.

You never change total_val.

Did you mean to call the function set_tot? You never call it.
Last edited on
Hi, thanks very much for the replies. If I don't set total_val, I get this output:
Stock holdings:
Company: Apple
Numshares: 10
Price: 50
Total price: -9.25596e+61
Company: Micro
Numshares: 7
Price: 3
Total price: -9.25596e+61

If I assign the value to p* n it works but I am trying to keep the calculation definition within the class because I am trying to break down another program to understand how it works, which is also the reason for the uncalled set_tot. This is the original 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
#ifndef STOCK20_H_	
#define STOCK20_H_	
#include <string>	
class Stock	
{	
private:	
	std::string company;
	int shares;
	double share_val;
	double total_val;
	void set_tot() { total_val = shares * share_val; }
public:	
	Stock(); // default constructor
	Stock(const std::string & co, long n = 0, double pr = 0.0);
	~Stock(); // do-nothing destructor
	void buy(long num, double price);
	void sell(long num, double price);
	void update(double price);
	void show()const;
	const Stock & topval(const Stock & s) const;
};	
#endif	

#include <iostream>	
#include "stock20.h"	
// constructors	
Stock::Stock() // default constructor	
{	
	company = "no name";
	shares = 0;
	
	total_val = 0.0;
}	
Stock::Stock(const std::string & co, long n, double pr)	
{	
	company = co;
	if (n < 0)
	{
	}
	else
	share_val = pr;
	set_tot();
}	
// class destructor	
Stock::~Stock() // quiet class destructor	
{	
}	
// other methods	
void Stock::buy(long num, double price)	
{	
	if (num < 0)
	{
	}
	else
	{
	}
}	
void Stock::sell(long num, double price)	
{	
	using std::cout;
	if (num < 0)
	{
	}
	else if (num > shares)
	{
	}
	else
	{
	}
}	
void Stock::update(double price)	
{	
	share_val = price;
	set_tot();
}	
void Stock::show() const	
{	
	using std::cout;
	using std::ios_base;
	// set format to #.###
	ios_base::fmtflags orig =
	std::streamsize prec = cout.precision(3);
	cout << "Company: " << company
	cout << " Share Price: $" << share_val;
	// set format to #.##
	cout.precision(2);
	cout << " Total Worth: $" << total_val << '\n';
	// restore original format
	cout.setf(orig, ios_base::floatfield);
	cout.precision(prec);
}	
const Stock & Stock::topval(const Stock & s) const	
{	
	if (s.total_val > total_val)
	else
}	

#include <iostream>		
#include "stock20.h"		
const int STKS = 4;		
int main()		
{		
	// create an array of initialized objects	
	Stock stocks[STKS] = {	
		Stock("NanoSmart", 12, 20.0),
		Stock("Boffo Objects", 200, 2.0),
		Stock("Monolithic Obelisks", 130, 3.25),
		Stock("Fleep Enterprises", 60, 6.5)
	};	
	std::cout << "Stock holdings:\n";	
	int st;	
	for (st = 0; st < STKS; st++)	
		stocks[st].show();
	// set pointer to first element	
	const Stock * top = &stocks[0];	
	for (st = 1; st < STKS; st++)	
		top = &top->topval(stocks[st]);
		// now top points to the most valuable holding
		std::cout << "\nMost valuable holding:\n";
	top->show();	
	return 0;	
}		
So what's the actual question, then?

You say that you're choosing to set total_val to zero, and you're choosing not to change it after that? So you're choosing for it to be zero, and it is zero? So what's the problem?
In the original program, line 87:
cout << " Total Worth: $" << total_val << '\n';
outputs the correct values. I set it to zero just as a way to initialise it. I can see that was unneeded. My question is how do I output the correct values, that is 500 and 21, for total price, using a similar style as the original program I am breaking down? That has at line 11:
void set_tot() { total_val = numshares * price; }
in the class and
cout << " Total Worth: $" << total_val << '\n';
in void Stock::show() const
Last edited on
The obvious way is to do something that sets total_val to the correct value.

The class contains a function you can call that will do that.

stocks[0].set_tot();
#include <iostream>
#include <string>
using namespace std;
class Stock
{
private:
string company;
int numshares;
double price;
public:
Stock(const string & co, long n = 0, double p = 0.0);
void show();
double total_val;
void set_tot() { total_val = numshares * price; }
};
const int numarr = 2;
int main()
{
Stock stocks[numarr] = { Stock("Apple", 10, 50.00),
Stock("Micro", 7, 3.00) };
cout << "Stock holdings:\n";
stocks[0].show();
stocks[1].show();
return 0;
}
Stock::Stock(const string & co, long n, double p)
{
company = co;
numshares = n;
price = p;
total_val = n*p;
}
void Stock::show()
{
cout << "Company: " << company << endl;
cout << "Numshares: " << numshares << endl;
cout << "Price: " << price << endl;
cout << "Total price: " << total_val << endl;
}


try this

This is the output:
Stock holdings:
Company: Apple
Numshares: 10
Price: 50
Total price: 500
Company: Micro
Numshares: 7
Price: 3
Total price: 21
Hi, I got it working in the style of the original program. Thank you very much for your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Stock::Stock(const string & co, long n, double p)
{
	company = co;
	numshares = n;
	price = p;
	set_tot();
}
void Stock::show()
{
	cout << "Company: " << company << endl;
	cout << "Numshares: " << numshares << endl;
	cout << "Price: " << price << endl;
	cout << "Total price: " << total_val << endl;
}
Last edited on
Topic archived. No new replies allowed.