Array that automatically gets size?

Pages: 12
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.


I would undo that modification.


Line 173 is where im trying to output the contents of the text file.


No, it's not.

Perhaps you could be a little more descriptive (and accurate) about the problem you're experiencing rather than just typing "bump."
Are you trying to...

1
2
3
4
5
while(!namesIn.eof())
{
    getline(namesIn, fileStr);
//    cout << fileStr << endl;
}


Anyways, why aren't you loading what is in the file into some container earlier in the program, then you just loop through that container printing each element?
Ok ill add more when I get home. Why do I need to undo that part?
Last edited on
Ok so here is 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
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
//Prison Simulator game by Chay Hawk
//Version pre-alpha 1.0.0
#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;

                cout << 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 With a Deadly Weapon",
        " - 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];
}


Ok so lines 174-179 is where im trying to output the contents of the text file, but it wont do it. When i go to that case file it shows the title but nothing else.
Ok so lines 174-179 ...


Again, no it's not. You open the output file on line 120. You close it on line 155. I suspect the output occurs somewhere in there.

How do you know it isn't outputting to the file? Have you checked to see if the file is generated and what its contents may be? What do you expect to happen when the switch is finished and it returns to the top of the loop?
Last edited on
Here is where im trying to view the contents of the text file. Im not trying to write stuff to the text fiel, im trying to view everything IN the text file in my program.

1
2
3
4
5
6
7
8
9
10
11
                cout << "CURRENT INMATES\n" << endl;

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

                cout << fileStr;
                namesOut.close();
                break;
        }
Last edited on
So, I misread. You're trying to read the contents of the text file in.

In the snippet you posted there, you:

Read a line into a string.
If there's more stuff in the file, you discard the contents of the string and read another line into the string.

Finally, when there's no more input in the file you display the contents of the last line you had extracted. All previous lines having been discarded.

Clanmjc provided the solution to that particular problem several posts ago -- see his comment in the code snippet, although it would be less error prone if you were to restructure the loop:

1
2
while (getline(namesIn, fileStr))
    cout << fileStr ;

Ok i got it working sort of, i had it in the load part of my code and when i mode it here

1
2
3
4
5
6
7
8
9
10
                cout << "CURRENT INMATES\n" << endl;

                namesIn.open("prisonersList.txt");

                while (getline(namesIn, fileStr));
                cout << fileStr;

                namesIn.close();
                break;
        }



it wont display anything.
NM i figured it out
But why was i suggested to take out the if and or of stream from the struct?
Logically, they don't belong there. Nor does randNames or randCrimes. I'm of the opinion that Game() doesn't belong there either, but you could make a case for it, I guess.

The file streams should be local to wherever they're being used.

For instance, your case 1: 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
               {
                    ofstream namesOut("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;
                    }
                }
                break; 


Note that the call to close() was eliminated as the destructor takes care of closing the file in this case.
Ok but when i take my function prototypes out, i get errors, when i try to fix them i get more errors. I tried removing Vars:: from in front of Game() and i got a ton more errors.

C:\Users\Chay Hawk\Desktop\Test project\main.cpp|87|error: no 'void Vars::Game()' member function declared in class 'Vars'|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|182|error: no 'std::string Vars::randNames()' member function declared in class 'Vars'|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|211|error: no 'std::string Vars::randCrimes()' member function declared in class 'Vars'|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|240|error: no 'std::string Vars::randDegrees()' member function declared in class 'Vars'|
||=== Build finished: 4 errors, 0 warnings ===|
So something like this is better?

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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//Prison Simulator game by Chay Hawk
//Version pre-alpha 1.0.0
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

void Game();
string randNames();
string randCrimes();
string randDegrees();

struct Vars
{
    long int money;
    int prisoners;
    string playerName;
    string prisonName;
    string fileStr;
};

int main()
{
    int choice;
    srand(time(0x0));
    ofstream fileOut;
    ifstream fileIn;

    Vars v;

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

    if(choice == 1) //New game
    {
        fileOut.open("prison.txt");

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

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

        cout << "\n";

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

        cout << "\n";

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

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

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

        fileOut.close();

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

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

        fileIn.close();

        Game();
    }
}

void Game()
{
    int choice;
    int random;
    int inMnum;
    bool endLoop = false;

    Vars v;

    ofstream namesOut;
    ifstream namesIn;

    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 != true)
    {
        cin >> choice;

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

                    cout << "INMATE LIST" << endl;

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

                    cout << "Enter how many inmates you would like to take in." << endl;
                    cout << "Your current prisoner count is: " << v.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());
                            degreeVect.push_back(randDegrees());
                        }

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

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

                namesIn.open("prisonersList.txt");

                while (getline(namesIn, v.fileStr))
                {
                    cout << v.fileStr << endl;
                }

                namesIn.close();
                break;
        }
    }
}

string 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 randCrimes()
{
    string crimes[20] =
    {
        " - Sexual Assault",
        " - Child Abuse",
        " - Murder",
        " - Assault With a Deadly Weapon",
        " - 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];
}

string randDegrees()
{
    string degrees[3] =
    {
        " - of the 1st degree",
        " - of the 2nd degree",
        " - of the 3rd degree"
    };

    return degrees[rand() % 3];
}
Topic archived. No new replies allowed.
Pages: 12