How to remember Variables?

Hello! I am trying to make a stock simulator, but I want to make the portfolio remember its money, or value, it has. Thank you in advance for whoever helps me!

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
//version v.1
#include <iostream>
using namespace std;

int main()
{
    int stock;
    float price;
    int portfolio=10000;
    char stockyn;
    char menu;
    int choice;

    cout << "Welcome to Stocking Stocks Simulator! This is your current portfolio: " << portfolio << " Current StockB$" << endl;
    cout << "Stock market crash! Inducers are rich!" << endl;
    cout << "Current stocks, Microsoft: each stock, 120 each.";
    cout << " Want to buy? Y or N?" << endl;
    cin >> stockyn;

    if (stockyn == 'Y')
    {
        cout << "Bought one trade! Current portfolio: " << portfolio - 120 << endl;

    }
    else if (stockyn == 'N')
    {
        cout << "Didn't buy trade, Current portfolio: "<< portfolio << endl;
    }
    cout << "Current Stocks, YAHOO, 100 each, ";
    cout << "Wanna buy? Y or N?" << endl;
    cin >> stockyn;

    if (stockyn == 'Y')
    {
        cout << "Bought a trade! Current portfolio: " << portfolio - 100 << endl;

    }
    else if (stockyn == 'N')
    {
        cout << "Didn't buy a trade, current portfolio: " << portfolio << endl;
    }
    cout << "What would you like to do next?" << endl;
    bool stockMenu = true;
    while (stockMenu != false){
        cout << "-----------------" << endl;
        cout << "1 - Check your portfolio." << endl;
        cout << "2 - Get a new stock partner." << endl;
        cout << "3 - Help." << endl;
        cout << "4 - Exit." << endl;

        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "Current portfolio: " << portfolio << endl;
            break;
        case 2:
            cout << "AOQL, 50 each, wanna buy?";
            cout << " Y or N?" << endl;
            cin >> stockyn;
            if (stockyn == 'Y'){
                cout << "Bought trade! Current portfolio: " << portfolio - 50 << endl;

            }
            else if (stockyn == 'N')
            {
                cout << "Didn't buy trade, current portfolio: " << portfolio << endl;
                cout << "Do you want to have another trade partner?" << endl;
                cin >> stockyn;
                if (stockyn = 'Y')
                {
                    cout << "Do you wanna buy AEX? 500 each, Y or N?" << endl;
                    cin >> stockyn;
                    if (stockyn == 'Y')
                    {
                        cout << "Bought! current portfolio: " << portfolio - 500 << endl;

                    }
                    else if (stockyn = 'N')
                    {
                        cout << "Didn't buy, current portfolio: " << portfolio << endl;
                        cout << "Would you like a different trade partner? Y or N." << endl;
                        cin >> stockyn;
                        if (stockyn == 'Y')
                        {
                            cout << "Would you like to buy from TOPAZ? 1000 each. Y or N?" << endl;
                            cin >> stockyn;
                            if (stockyn == 'Y')
                            {
                                cout << "Trade bought! Current portfolio: " << portfolio - 1000 << endl;

                            }
                            else if (stockyn == 'N')
                            {
                                cout << "Trade cancelled, more trades soon!" << endl;

                            }

                        }


                    }


                }

            }
            break;
        case 3:
            cout << "Just search up some tutorials lol." << endl;
        case 4:
            cout << "Exiting the program.." << endl;
            stockMenu = false;
            break;
            default:
            cout << "Not a vaild choice." << endl;
            cout << "Choose again!";
            cin >> choice;
            break;
        }
    }

return 0;
}
variables retain their value until the program ends.
if you want to remember values when the program is not running, you want a disk file.
a simple text file works a lot like cin / cout.

ifstream ifs("filename.txt");
ifs << variable << endl; //write

or
ofstream ofs("filename.txt");
ofs >> variable; //read

research ifstream and ofstream for some better code examples and you should be able to work them into your code.
Registered users can post here. Sign in or register to post.