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.
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.
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. :-)
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.
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 . . .