Trouble with Arrays

Hi,
I have an assignment to make a "mock stock exchange" in which there is a person with an account of 10 dollars and they can choose to buy and sell shares in stocks. I'm running into a problem in which I need the stockID, stockName, and stockPrice (at time of purchase) to save into separate arrays to be displayed when z=='F'.

Please don't pick apart every part of my program, it is very much an unfinished project; but, to continue with it on my own, I have to get past this problem.

Thanks for your help!

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
  #include <string>
#include <iostream>
using namespace std;
int main()
{
    float liz=10;
    int stocks[5];
    int stockID[5];
    int stockPrice[5];
    int lizPlaceStocks[5];
    int lizPlaceStockID[5];
    int lizPlaceStockPrice[5];
    int i;
    char z;
    cout<<"Select one of the following: "<<endl;
    cout<<"A: To see all the stocks and their prices: "<<endl;
    cout<<"B: To Purchase a Stock: "<<endl;
    cout<<"C: To see how much money is in Liz: "<<endl;
    cout<<"D: To change the price of the stocks by 10%: "<<endl;
    cout<<"F: To show the current position: "<<endl;
    cout<<"G: To Sell a Stock: "<<endl;
    cout<<"H: To Calculate Profit: "<<endl;
    cout<<"I: To Save Current Position: "<<endl;
    cout<<"J: Query Asset: "<<endl;
    cout<<"E: To Exit the Program: "<<endl;
    cin>>z;
    for (i=0; i<5; i++)
    {
        stockPrice[i]=rand()%10;
        if (stockPrice[i]==0)
        {
            stockPrice[i]=rand()%10;
        }
        stockID[i]=i;
        stocks[i]=i+1;
    }
    while (z!='E')
    {
        if (z=='A') //Lists the stocks in order of ID, Name and Price
            {
                cout<<"Stock ID"<<"--"<<"Stock Name"<<"--"<<"Stock Price"<<endl;
                for (i=0; i<5; i++)
                    {
                        cout<<stockID[i]<<"--"<<stocks[i]<<"--"<<stockPrice[i]<<endl;
                    }
            }
        if (z=='B') // To buy stocks
            {
                int x, y, liz2;
                liz2=liz;
                cout<<"Enter the stock ID you would like to buy: "<<endl;
                cin>>x;
                x=stockID[x];
                cout<<"This stock costs: $"<<stockPrice[x]<<endl;
                cout<<"How many shares would you like to buy? ";
                cin>>y;
                liz=liz-(stockPrice[x]*y);
                while (liz<=-1) // Loop for saying there is not enough money to purchase that many shares
                    {
                        cout<<"You do not have enough money in your account to make this purchase"<<endl;
                        cout<<"Your account is reverted back to $"<<liz2<<endl;
                        cout<<"If you no longer would like to make a purchase, enter 0 for the next two inputs"<<endl;
                        liz=liz2;
                        cout<<"Enter the stock ID you would like to buy: "<<endl;
                        cin>>x;
                        x=stockID[x];
                        cout<<"This stock costs: $"<<stockPrice[x]<<endl;
                        cout<<"How many shares would you like to buy? ";
                        cin>>y;
                        liz=liz-(stockPrice[x]*y);
                    }
                if (liz>=0) // Saying that she has enough money in her account and the purchase is valid
                    {
                        cout<<"Your balance after the purchase is: $"<<liz<<endl;
                        for (i=0; i<stockPrice[i]; i++)
                        {
                            lizPlaceStockPrice[i]=stockPrice[i];
                            lizPlaceStocks[i]=stocks[i];
                            lizPlaceStockID[i]=stockID[i];
                        }
                    }
            }
        if (z=='C') // Test -- making sure code runs correctly
            {
                cout<<liz<<endl;
            }
        if (z=='D') // Changes the price of the stocks by 10%
            {
                for (i=0; i<5; i++)
                {
                    stockPrice[i]=(stockPrice[i]*rand()%10);
                    if (stockPrice[i]==0)
                        {
                            stockPrice[i]=rand()%10;
                        }
                }
            }
        if (z=='F') // Is supposed to show ID, Name, and Price of stocks that liz owns (DONT KNOW HOW TO DO)
            {
                cout<<lizPlaceStockID[i]<<"--"<<lizPlaceStocks[i]<<"--"<<lizPlaceStockPrice[i]<<endl;
            }
        if (z=='G') //still need to enter the "You do not own this many shares" or "You do not own this stock" element
            {
                int a, b;
                cout<<"Enter the stock ID you would like to sell: "<<endl;
                cin>>a;
                a=stockID[a];
                cout<<"How many shares would you like to sell: "<<endl;
                cin>>b;
                liz=liz+(stockPrice[a]*b);
            }
        if (z=='H') //Need to figure out how to save the prices of the stocks bought and sold in a new array
            {
                int profit;
                profit = 10+liz;
                cout<<profit;
            }
        if (z=='I') //Save to text file, but first need to save information to a new array
            {
                
            }
        if (z=='Z')
        {
            for (i=0; i<lizPlaceStockID[i]; i++)
            {
                cout<<lizPlaceStockID[i];
            }
        }
        cout<<"Select one of the following: "<<endl;
        cout<<"A: To see all the stocks and their prices: "<<endl;
        cout<<"B: To Purchase a Stock: "<<endl;
        cout<<"C: To see how much money is in Liz: "<<endl;
        cout<<"D: To change the prices of the stocks by 10%: "<<endl;
        cout<<"F: To show the current position: "<<endl;
        cout<<"G: To Sell a Stock: "<<endl;
        cout<<"H: To Calculate Profit: "<<endl;
        cout<<"I: To Save Current Position: "<<endl;
        cout<<"J: Query Asset: "<<endl;
        cout<<"E: To Exit the program: "<<endl;
        cin>>z;
       // for (i=0; i<lizPlaceStockID[i]; i++)
       // {
       //     lizPlaceStockID[i]=stockID[i];
       //     lizPlaceStocks[i]=stocks[i];
       //     lizPlaceStockPrice[i]=stockPrice[i];
       // }
    }
}
Hello mtroll,

I would check into line 100 for you problem. You have three arrays all idexed with the variable 'i', but where was 'i' set? When I ran the program 'i' had a value of 5 which is outside the bounds of the arrays, so I was getting garbage for the value of index 5.

I would also suggest that your for loops start with int i = 0; which should give 'i' local scope in the for loop and meaning that the variable 'i' at line 13 will need to be set before it is used like in line 100.

It also looks like all the arrays in line 100 have no values. Not sure where they have been given a value yet. All your variables from line 7 thru 14 should be initialized.

That is a start. Hope that helps,

Andy
Last edited on
Hi Andy,

I was getting a bit aggravated so I decided to start from scratch and actually fixed of problem as mentioned before. But of course that means I ran into a new one. My current code:

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
#include <string>
#include <iostream>
using namespace std;
class stockholder
{
public:
    int stockID[5];
    int stockName[5];
    int stockPrice[5];
    int account=10;
    int stockShares[5];
};
int main()
{
    stockholder mike, liz, colin;
    mike.account=10;
    liz.account=10;
    colin.account=10;
    int stockPrice[5];
    int stockID[5];
    int stocks[5];
    int stockShares[5];
    int i;
    char z;
    for (i=0; i<5; i++)
    {
        stockPrice[i]=rand()%10;
        if (stockPrice[i]==0)
        {
            stockPrice[i]=rand()%10;
        }
        stockID[i]=i;
        stocks[i]=i+1;
    }

    cout<<"Enter 1: For Mike -- Enter 2: For Liz -- Enter 3: For Colin"<<endl;;
    cin>>z;
    while (z!='E')
    {
        if (z=='1')
            {
                cout<<"You chose Mike"<<endl;
                while (z!='X')
                {
                    cout<<"Select A: To show the stocks: "<<endl;
                    cout<<"Select B: To Purchase a Stock as Mike: "<<endl;
                    cout<<"Select C: To Show the Stocks Owned by Mike: "<<endl;
                    cin>>z;
                    if (z=='A') //Lists the stocks in order of ID, Name and Price
                    {
                        cout<<"Stock ID"<<"--"<<"Stock Name"<<"--"<<"Stock Price"<<endl;
                        for (i=0; i<5; i++)
                        {
                            cout<<stockID[i]<<"--"<<stocks[i]<<"--"<<stockPrice[i]<<endl;
                        }
                    }
                    if (z=='B')
                    {
                        int x, y, mike2;
                        mike2=mike.account;
                        cout<<"Enter the stock ID you would like to buy: "<<endl;
                        cin>>x;
                        x=stockID[x];
                        cout<<"This stock costs: $"<<stockPrice[x]<<endl;
                        cout<<"How many shares would you like to buy? ";
                        cin>>y;
                        mike.account=mike.account-(stockPrice[x]*y);
                        stockShares[y]=y;
                        while (mike.account<=-1) // Loop for saying there is not enough money to purchase that many shares
                        {
                            cout<<"You do not have enough money in your account to make this purchase"<<endl;
                            cout<<"Your account is reverted back to $"<<mike2<<endl;
                            cout<<"If you no longer would like to make a purchase, enter 0 for the next two inputs"<<endl;
                            mike.account=mike2;
                            cout<<"Enter the stock ID you would like to buy: "<<endl;
                            cin>>x;
                            x=stockID[x];
                            cout<<"This stock costs: $"<<stockPrice[x]<<endl;
                            cout<<"How many shares would you like to buy? ";
                            cin>>y;
                            mike.account=mike.account-(stockPrice[x]*y);
                        }
                        if (mike.account>=0) // Saying that he has enough money in her account and the purchase is valid
                        {
                            cout<<"Your balance after the purchase is: $"<<mike.account<<endl;
                            for (i=0; i<1; i++)
                            {
                                mike.stockName[i]=stocks[x];
                                mike.stockID[i]=stockID[x];
                                mike.stockPrice[i]=stockPrice[x];
                                mike.stockShares[i]=stockShares[y];
                            }
                        }
 
                    }
                    if (z=='C')
                    {
                        cout<<"StockID -- Stock Name -- Stock Price -- Shares"<<endl;
                        for (i=0; i<5; i++)
                        {
                            cout<<mike.stockID[i]<<"--"<<mike.stockName[i]<<"--"<<mike.stockPrice[i]<<"--"<<mike.stockShares[i]<<endl;
                        }
                    }
                }

            }
        cout<<"Select A: To List the Stocks"<<endl;
        cout<<"Select B: To Purchase a Stock as Mike: "<<endl;
        cout<<"Select C: To Show the Stocks owned by Mike: "<<endl;
        cin>>z;
    }
}



Properly sends the information into my class arrays. My problem now is when someone wants to buy more than one stock, the information only goes to class[0]. I need the first bought stock to go to class[0], the second bought stock to go to class[1] and so on... do you have any ideas?

Thanks for your help,
Mike
Last edited on
You need to keep track of how many stocks each shareholder has purchased.
Each time a shareholder purchases a stock, increment that counter. That counter then becomes your index.

BTW, you really need to use an array of stockholders. Are you going to repeat your entire program for liz and colin? What happens when you want to add another stockholder?

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

How exactly do you do a counter such as the one you are suggesting with arrays? I haven't had any experience with counters except with for() loops.
Last edited on
1) I decided to do if statements solely because I am better practiced in them and this is a trial run of the code

2) As previously stated, this is just a trial, I will have to change things before I actually submit the assignment, such as making it so 'A' and 'a' work for the if statements.

That still leaves the question on how I do a counter as described above since I am not familiar with anything like that.
The concept of a counter variable is very simple. As I said before:
Each time a shareholder purchases a stock, increment that counter. That counter then becomes your index.
1
2
3
4
5
6
7
8
9
10
class stockholder
{
public:
    int num_stocks = 0;  // Counter/index variable 
    int stockID[5];
    int stockName[5];
    int stockPrice[5];
    int account=10;
    int stockShares[5];
};


Why is stockName an array of 5 ints?

To add a stock:
1
2
3
4
5
  mike.stockID[mike.num_stocks] = stock_id;
  mike.stockPrice[mike.num_stocks] = price;
  mike.stockShares[mike.num_stocks] = shares;
  mike.account -= shares * price;
  mike.num_stocks++;  // Increment number of stocks held 


As I pointed out before, you have a major design flaw in your program. You have to recode your program every time you want to add a shareholder. Very poor design. Consider that every shareholder has a name. Name should be a member of the shareholder class. It should NOT be a variable name (i.e. mike).

Second, since you have a shareholder class, you should have member functions that deal with your class. Consider the function of adding a stock to a shareholder. That code belongs in your class, not in main.

1
2
3
4
5
6
7
8
9
10
11
12
class stockholder
{
public:
    int num_stocks = 0;  // Counter/index variable 
    int stockID[5];
    int stockName[5];
    int stockPrice[5];
    int account=10;
    int stockShares[5];

    void add_stock (int stockid, int price, int shares);
};


Now, when you want to add a stock, you simply call that function:
1
2
3
4
5
6
7
8
9
void stockholder::add_stock (int stockid, int price, int shares)
{  stockID[num_stocks] = stockid;
    stockPrice[num_stocks] = price;
    stockShares[num_stocks] = shares;
    account -= shares * price;
    num_stocks++;  // Increment number of stocks held
}

  mike.add_stock (123, 3, 2);  //  Add 2 shares of stock 123 at price 3 


Edit: You should also consider a Stock class. Stocks and shareholders are two different kinds of objects. A shareholder HAS stocks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Stock  
{   int stockID;
    int stockPrice;
public:
    Stock (int id, int price);       
};

class Shareholder
{   string      name;   
    int         num_stocks;
    Stocks      stocks[5];
    int         balance;
            
public:
    Stockholder (string _name);
    void AddStock (Stock stock);
};
Last edited on
Thank you so much for your help! This is exactly what I needed.
Topic archived. No new replies allowed.