How to print (display to screen) an array of struct

I actually have two problems (maybe more?) here. I think the more difficult one is that

I don't know how to display the information in the array of struct

and

I can't seem to figure out how to create a "value of total shares" for each company. It should be calculated using stock.shares * stock.price.

but no matter where I try to add it, I getsome sort of error. Where it's located in the following code the error is "data member initializer not allowed"

Please help, I'm trying hard to learn C++ but I keep getting stuck.

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
#include <iostream>
using namespace std;

//structure declaration
struct stock
{
	char company [50];
	int shares;
	float price;
	float value = shares * price
};

const int SizeOfArray = 4;

void main()
{
stock stock[SizeOfArray];
	for(int i = 0; i<4; i++)           //stops when i=4, 4 companies entered
{	cout<<"Enter the Company Name: ";
	cin>>stock[i].company;
	cout<<"Enter the share price: ";
	cin>>stock[i].price;
	cout<<"Enter the amount of shares: ";
	cin>>stock[i].price;
	cout<<endl;
	}
cout<<stock[];
}
You cant have a class name and object name be the same
 
stock Stock;
should fix your first problem

also you need to loop through to print the stock
line 27 should probably be
1
2
3
4
for (int i = 0; i < 4; ++i)
{
   cout << "Stock price [" << i << "]: " << stock[i].price << endl;
}
Last edited on
you can't do like this float value = shares * price in structure..
After getting all values write code for calculating total values..
1
2
3
4
for (int i = 0; i < 4; ++i)
{
   stock[i].value = stock[i].shares * stock[i].price 
}
Or you can create a member function of structure for value calculation;
Other then that you can create one more function to print the structure members, as you are trying to print the structure itself cout<<stock[]; which is incorrect.

Ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct stock
{
        char company [50];
        int shares;
        float price;
        float value;

        void setValues( )
        {
                value = shares * price;
        }

        void PrintStructure()
        {
                cout << "Company: " << company;
                cout << "Shares : " << shares ;
                cout << "Price: " << price;
                cout << "Value: " << value;
        }
};


now use stock[index].PrintStructure();
Last edited on
This community is amazing. Thank you Angeljruiz, HiteshVaghani1, and upX86 for your quick responses.

I made the changes. I'm posting the corrected code below. I am getting 2 errors during build:

...cpp(38): error C2065: 'index' : undeclared identifier

...cpp(38): error C2228: left of '.PrintStructure' must have class/struct/union

Can anyone shed light on these?

Thanks again.

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
#include <iostream>
using namespace std;

//structure declaration
struct stock
{
	char company [50];
	int shares;
	float price;
	float value;
	
	void setValues()
	{
		value = shares * price;
	}
	void PrintStructure()
	{
		cout << company << "   " << shares <<"   "<< price << "   " << value << endl;
	}
};

const int SizeOfArray = 4;

void main()
{
stock Stock[SizeOfArray];
	for(int i = 0; i<4; i++)           //stops when i=4, 4 companies entered
	{	
	cout<<"Enter the Company Name: ";
	cin>>Stock[i].company;
	cout<<"Enter the share price: ";
	cin>>Stock[i].price;
	cout<<"Enter the amount of shares: ";
	cin>>Stock[i].price;
	cout<<endl;
	}

Stock[index].PrintStructure();
};
Line 38
You need to declare the index.

BTW:
Why are you not using

1
2
3
4
5
6

int main()
{

return 0;
}
You are getting the error messages because you have not defined what index is. In order to print the information for every struct in the Stock array, you need to loop through each member using the PrintStructure() function for each one.

You'll need to initialize an index variable (to keep track of which member of the array you are currently accessing).

You'll need to create a loop which increases that index variable with each iteration while also calling the

Stock[index].PrintStructure();

And stopping once the index variable is equal to the max array length.

As mentioned earlier, the for loop is probably the best option
Thank you Kart and Roastern, I read through the Arrays section of the documentation here and think I've made the changes you suggested.

It builds and outputs the results but my "value" for is giberish. I tried several things, like using a cin>>value inside the Stock array, but that basically breaks the program.

Any ideas why the value is not computing?

I also have to come up with a total portfolio value, basically a sum of values. Haven't even thought where to start with that yet. I have to get company values working first I guess.

Here's my new code.

Thank you again for all of your suggestions and help with this.

I'm starting to feel like the more I learn the more I need to learn. :-)

This is the output I get:

abc 1 1 -1.07374e+008
bcd 2 2 -1.07374e+008
cde 3 3 -1.07374e+008
def 4 4 -1.07374e+008

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
#include <iostream>
using namespace std;

//structure declaration
struct stock
{
	char company [50];
	int shares;
	float price;
	float value;
	
	void setValues()
	{
		value = shares * price;
	}
	void PrintStructure()
	{
		cout << company << "   " << shares <<"   "<< price << "   " << value << endl;
	}
};

const int SizeOfArray = 4;


int main()
{
stock Stock[SizeOfArray];
	for(int i = 0; i<4; i++)           //stops when i=4, 4 companies entered
	{	
	cout<<"Enter the Company Name: ";
	cin>>Stock[i].company;
	cout<<"Enter the share price: ";
	cin>>Stock[i].shares;
	cout<<"Enter the amount of shares: ";
	cin>>Stock[i].price;
	cout<<endl;
	}
	
	for(int i = 0; i < 4; i++)
	{
	Stock[i].PrintStructure();
	}
return 0;
};
You're getting there. In the struct stock, value is undefined until the function setValues() is ran.

So you will have to loop through the array running that function for each element (or you could run the function in one of the loops you already have so you don't have to make another loop just for that purpose).

Adding a total value will also be easy to implement and can be handled in either of the loops you currently have as well.
Thanks Roastern! That got my value computing and outputting correctly!

I think I'm making a mess of the total value though. I've been changing it for a couple of hours now and haven't been able to get it right.

This is my latest code. This builds but gives giberish for the PortfolioValue.

Am I even on the right track with this? I thought the total value would be something more straight forward that added all the stock values of the array.

It seems too complicated the way I added it to line 11, 17, 42 and 50. :-( Besides that it doesn't give the correct answer.

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
#include <iostream>
using namespace std;

//structure declaration
struct stock
{
	char company [50];
	int shares;
	float price;
	float value;
	float PortfolioValue;

	void setValues()
	{
		value = shares * price;
	}
	void setPortfolioValue()
	{
		PortfolioValue += value;
	}
	void PrintStructure()
	{
		cout << company << "   " << shares <<"   "<< price << "   " << value << endl;
	}
};

const int SizeOfArray = 4;


int main()
{
stock Stock[SizeOfArray];
	for(int i = 0; i<4; i++)           //stops when i=4, 4 companies entered
	{	
	cout<<"Enter the Company Name: ";
	cin>>Stock[i].company;
	cout<<"Enter the share price: ";
	cin>>Stock[i].shares;
	cout<<"Enter the amount of shares: ";
	cin>>Stock[i].price;
	Stock[i].setValues();
	Stock[i].setPortfolioValue();
	cout<<endl;
	}
	
	for(int i = 0; i < 4; i++)
	{
	Stock[i].PrintStructure();
	}
cout<<"Total Portfolio Value:  "<<Stock[3].PortfolioValue<<endl;

return 0;
};
I am getting the correct output! I have no idea how clumsy/clutzy my code is but thanks to all of you, your instructions and suggestions, I finally got the code to work.

Thank you!

output:
Enter the Company Name: abc
Enter the share price: 1
Enter the amount of shares: 1

Enter the Company Name: bcd
Enter the share price: 2
Enter the amount of shares: 2

Enter the Company Name: cde
Enter the share price: 3
Enter the amount of shares: 3

Enter the Company Name: def
Enter the share price: 4
Enter the amount of shares: 4

abc   1   1   1
bcd   2   2   4
cde   3   3   9
def   4   4   16
Total Portfolio Value:  30
Press any key to continue . . .


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
#include <iostream>
using namespace std;

//structure declaration
struct stock
{
	char company [50];
	int shares;
	float price;
	float value;
//	float PortfolioValue;

	void setValues()
	{
		value = shares * price;
	}
	//void setPortfolioValues()
	//{
		//PortfolioValue += value;
	//}
	void PrintStructure()
	{
		cout << company << "   " << shares <<"   "<< price << "   " << value << endl;
	}
};

const int SizeOfArray = 4;


int main()
{
float PortfolioValue = 0;
stock Stock[SizeOfArray];
	for(int i = 0; i<4; i++)           //stops when i=4, 4 companies entered
	{	
	cout<<"Enter the Company Name: ";
	cin>>Stock[i].company;
	cout<<"Enter the share price: ";
	cin>>Stock[i].shares;
	cout<<"Enter the amount of shares: ";
	cin>>Stock[i].price;
	Stock[i].setValues();
	PortfolioValue += Stock[i].value;
//	Stock[i].PortfolioValue += Stock[i].value;
	//Stock[i].setPortfolioValues();
	cout<<endl;
	}

	for(int i = 0; i < 4; i++)
	{
	Stock[i].PrintStructure();
	}
cout<<"Total Portfolio Value:  "<<PortfolioValue<<endl;

return 0;
};
Topic archived. No new replies allowed.