Array that automatically gets size?

Pages: 12
Ok so i keep adding stuff to my array but i want to know is there a way that the array can automatically get its size without me having to change the number in the brackets? in a catch statement you can put (...) to get any type of data type, is something like that possible for an array?
Not sure I understand your question fully.

If you want a dynamically sized array, why don't you use a STL vector?
Last edited on
Such array is called std::vector.:)
What i was saying is, is there a way i can add elements to the array without having to put a number of how many elements ther is.
ogh a vector can do that?
I forget, do i need to put something on the end of my array? something like '0\' or something like that to specify its the end or something?
No, you're thinking of old C strings which were, in essence, null-terminated character arrays.

You can add to a vector by pushing to it and return the size using the size() member function.
Ah i see, ok well i'll stick with arrays. But i have a problem with my code:

I have a while loop that is supposed to read the contents of a text file and load it itno the game so it can be read later (its a list of prisoners) but when i pick option 2 the program just stops and doesnt do anything else. If i comment out the while loop:

1
2
3
4
 while(!v.names.eof())
        {
            getline(names, v.fileStr);
        }


Then pick option 2 it loads everything else just fine so its that part thats causing problems, what is doing that?

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
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

struct Vars
{
    void Game();
    string randNames();
    string randCrimes();

    long int money;
    int prisoners;
    string playerName;
    string prisonName;
    ofstream names; // Saves the prisoners list
    ofstream file; // Saves the player info
    string fileStr;
};

int main()
{
    int choice;
    srand(time(0x0));

    Vars v;

    cout << "1) New" << endl;
    cout << "2) Load\n" << endl;
    cin >> choice;

    if(choice == 1)
    {
        v.file.open("prison.txt");

        cin.ignore(1000, '\n');

        cout << "Hello please enter your name" << endl;
        getline(cin, v.playerName);
        v.file << v.playerName << endl;

        cout << "\n";

        cout << "Thank you " << v.playerName << " now please enter the name of your prison" << endl;
        getline(cin, v.prisonName);
        v.file << v.prisonName << endl;

        cout << "\n";

        cout << "Ok thank you lets start the game" << endl;
        cin.get();

        v.money = 50000;
        v.prisoners = 0;

        v.file << v.money << endl;
        v.file << v.prisoners << endl;

        v.file.close();

        v.Game();
    }
    else if(choice == 2)
    {
        ifstream file;
        ifstream names;

        file.open("prison.txt");
        names.open("prisonersList.txt");

        getline(file, v.playerName);
        getline(file, v.prisonName);
        file >> v.money;
        file >> v.prisoners;
        
        while(!v.names.eof())
        {
            getline(names, v.fileStr);
        }

        file.close();

        v.Game();
    }
}
As I said in your other post, it's because you're getting a little mixed up with streams.

If you're reading from a file, you want an ifstream object, not an ofstream.
Ah ok i see it now. Now i have another problem. When i pick option 2, it clears the prisoners text file of all text, why is that?

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
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

struct Vars
{
    void Game();
    string randNames();
    string randCrimes();

    long int money;
    int prisoners;
    string playerName;
    string prisonName;
    ofstream names; // Saves the prisoners list
    ofstream file; // Saves the player info
    string fileStr;
};

int main()
{
    int choice;
    srand(time(0x0));

    Vars v;

    cout << "1) New" << endl;
    cout << "2) Load\n" << endl;
    cin >> choice;

    if(choice == 1)
    {
        v.file.open("prison.txt");

        cin.ignore(1000, '\n');

        cout << "Hello please enter your name" << endl;
        getline(cin, v.playerName);
        v.file << v.playerName << endl;

        cout << "\n";

        cout << "Thank you " << v.playerName << " now please enter the name of your prison" << endl;
        getline(cin, v.prisonName);
        v.file << v.prisonName << endl;

        cout << "\n";

        cout << "Ok thank you lets start the game" << endl;
        cin.get();

        v.money = 50000;
        v.prisoners = 0;

        v.file << v.money << endl;
        v.file << v.prisoners << endl;

        v.file.close();

        v.Game();
    }
    else if(choice == 2)
    {
        ifstream file;
        ifstream names;

        file.open("prison.txt");

        getline(file, v.playerName);
        getline(file, v.prisonName);
        file >> v.money;
        file >> v.prisoners;

        file.close();

        names.open("prisonersList.txt");

        while(!names.eof())
        {
            getline(names, v.fileStr);
        }

        names.close();

        v.Game();
    }
}
Last edited on
Not sure. Can't see any reason why it would be doing that. Nothing in the code seems to suggest that it would be doing that for option two.

One thing I will say, though, is that you're constantly overwriting the fileStr string of the Vars class. It's only ever going to store the last line of text from the prisonersList.txt file.
How is it being constantly overwritten? because its in the while loop?
Oh and i still cant figure out whats wrong with the file being overwritten i tried a few things but nothing. :(
When you use getline, the string will get a new value so the old value is lost.
I tried not using getline and the contents of my text file were still deleted.
Do you write to the file that is being deleted somewhere in the program?
I think but im not sure. Maybe lines 51 and 52?
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
void Vars::Game()
{
    int choice;
    int random;
    int inMnum;
    bool endLoop = true;
    names.open("prisonersList.txt");

    cout << "Main Menu\n" << endl;

    cout << "What do you want to do?\n" << endl;

    cout << "1) Add prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison statistics" << endl;
    cout << "4) Execute Prisoner(s)" << endl;
    cout << "5) List of current inmates" << endl;

    while(endLoop != false)
    {
        cin >> choice;

        switch(choice)
        {
            case 1:
                {
                    cout << "INMATE LIST" << endl;

                    vector<string> nameVect;
                    vector<string> crimeVect;

                    cout << "Enter how many inmates you would like to take in." << endl;
                    cout << "Your current prisoner count is: " << prisoners << endl;
                    cout << "You may not exceed 25 prisoners at this time" << endl;
                    cin >> inMnum;

                    if((inMnum > 0) && (inMnum < 26))
                    {
                        for(int i = 0; i < inMnum; i++)
                        {
                            nameVect.push_back(randNames());
                            crimeVect.push_back(randCrimes());
                        }

                        cout << "Name " << "Crime\n" << endl;

                        for(int i = 0; i < inMnum; i++)
                        {
                            cout << nameVect[i] << " ";
                            cout << crimeVect[i] << endl;
                            names << nameVect[i] << " ";
                            names << crimeVect[i] << endl;
                        }
                    }
                    else
                    {
                        cout << "Error" << endl;
                        endLoop = false;
                    }
                }
                break;
            case 2:
                cout << "Executed Prisoners\n" << endl;
                break;
            case 3:
                cout << "Prison Statistics\n" << endl;
                cout << "Warden Name: " << playerName << endl;
                cout << "Prison Name: " << prisonName << endl;
                cout << "Current Money: " << money << endl;
                cout << "Prisoners: " << prisoners << endl;
                break;
            case 4:
                cout << "EXECUTE A PRISONER\n" << endl;
                break;
            case 5:
                cout << "CURRENT INMATES\n" << endl;

                cout << fileStr;
                break;
        }
    }

}
Last edited on
Ik i think i fixed it finally.
Now i need to know how i output it?

EDIT: Ok i modified the program so that if and of stream are in the struct, the code works now, i just cant outpur whats in my text file.

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

struct Vars
{
    void Game();
    string randNames();
    string randCrimes();

    long int money;
    int prisoners;
    string playerName;
    string prisonName;
    ofstream namesOut; // Saves the prisoners list
    ofstream fileOut; // Saves the player info
    ifstream fileIn;
    ifstream namesIn;
    string fileStr;
};

int main()
{
    int choice;
    srand(time(0x0));

    Vars v;

    cout << "1) New" << endl;
    cout << "2) Load\n" << endl;
    cin >> choice;

    if(choice == 1)
    {
        v.fileOut.open("prison.txt");

        cin.ignore(1000, '\n');

        cout << "Hello please enter your name" << endl;
        getline(cin, v.playerName);
        v.fileOut << v.playerName << endl;

        cout << "\n";

        cout << "Thank you " << v.playerName << " now please enter the name of your prison" << endl;
        getline(cin, v.prisonName);
        v.fileOut << v.prisonName << endl;

        cout << "\n";

        cout << "Ok thank you lets start the game" << endl;
        cin.get();

        v.money = 50000;
        v.prisoners = 0;

        v.fileOut << v.money << endl;
        v.fileOut << v.prisoners << endl;

        v.fileOut.close();

        v.Game();
    }
    else if(choice == 2)
    {
        v.fileIn.open("prison.txt");

        getline(v.fileIn, v.playerName);
        getline(v.fileIn, v.prisonName);
        v.fileIn >> v.money;
        v.fileIn >> v.prisoners;

        v.fileIn.close();

        v.namesIn.open("prisonersList.txt");

        while(!v.namesIn.eof())
        {
            getline(v.namesIn, v.fileStr);
        }

        v.namesIn.close();

        v.Game();
    }
}

void Vars::Game()
{
    int choice;
    int random;
    int inMnum;
    bool endLoop = true;

    cout << "Main Menu\n" << endl;

    cout << "What do you want to do?\n" << endl;

    cout << "1) Add prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison statistics" << endl;
    cout << "4) Execute Prisoner(s)" << endl;
    cout << "5) List of current inmates" << endl;

    while(endLoop != false)
    {
        cin >> choice;

        switch(choice)
        {
            case 1:
                {
                    namesOut.open("prisonersList.txt");

                    cout << "INMATE LIST" << endl;

                    vector<string> nameVect;
                    vector<string> crimeVect;

                    cout << "Enter how many inmates you would like to take in." << endl;
                    cout << "Your current prisoner count is: " << prisoners << endl;
                    cout << "You may not exceed 25 prisoners at this time" << endl;
                    cin >> inMnum;

                    if((inMnum > 0) && (inMnum < 26))
                    {
                        for(int i = 0; i < inMnum; i++)
                        {
                            nameVect.push_back(randNames());
                            crimeVect.push_back(randCrimes());
                        }

                        cout << "Name " << "Crime\n" << endl;

                        for(int i = 0; i < inMnum; i++)
                        {
                            cout << nameVect[i] << " ";
                            cout << crimeVect[i] << endl;
                            namesOut << nameVect[i] << " ";
                            namesOut << crimeVect[i] << endl;
                        }
                    }
                    else
                    {
                        cout << "Error" << endl;
                        endLoop = false;
                    }
                    namesOut.close();
                }
                break;
            case 2:
                cout << "Executed Prisoners\n" << endl;
                break;
            case 3:
                cout << "Prison Statistics\n" << endl;
                cout << "Warden Name: " << playerName << endl;
                cout << "Prison Name: " << prisonName << endl;
                cout << "Current Money: " << money << endl;
                cout << "Prisoners: " << prisoners << endl;
                break;
            case 4:
                cout << "EXECUTE A PRISONER\n" << endl;
                break;
            case 5:
                cout << "CURRENT INMATES\n" << endl;

                while(!namesIn.eof())
                {
                    getline(namesIn, fileStr);
                }
                break;
        }
    }

}

string Vars::randNames()
{
    string names[20] =
    {
        "Jerry",
        "Mike",
        "Phill",
        "Robert",
        "Allen",
        "Alex",
        "Tim",
        "Steven",
        "Ronald",
        "David",
        "Harold",
        "Thomas",
        "Andrew",
        "Carlito",
        "Frank",
        "Dale",
        "John",
        "Mark",
        "Josh",
        "Martin"
    };

    return names[rand() % 20];
}

string Vars::randCrimes()
{
    string crimes[20] =
    {
        " - Sexual Assault",
        " - Child Abuse",
        " - Murder",
        " - Assault",
        " - Breaking and Entering",
        " - Domestic Violence",
        " - Unlawful Posession of a Firearm",
        " - Destruction of private property",
        " - Kidnapping",
        " - Armed Robbery",
        " - Petty Theft",
        " - Disorderly Conduct",
        " - Indecent Exposure",
        " - Stalking",
        " - Shoplifting",
        " - Trespassing",
        " - Battery on and Officer",
        " - Forgery",
        " - Grand Theft Auto",
        " - Probation Violation"
    };

    return crimes[rand() % 20];
}


Line 173 is where im trying to output the contents of the text file.
Last edited on
bump.
Pages: 12