Whats going on with this switch statement?

Ok i have no idea whats going on here, it wont let me have case 2: or case 3: in my 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
void Vars::Game()
{
    int choice;

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

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

    cout << "1) View list of prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison statistics" << endl;
    cout << "4) Execute Prisoner(s)" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            cout << "Inmate list, maximum 25 inmates\n" << endl;

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

            for(int i = 0; i < 25; i++)
            {
                nameVect.push_back(randNames());
                crimeVect.push_back(randCrimes());
            }

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

            for(int i = 0; i < 25; i++)
            {
               cout << nameVect[i] << " ";
               cout << crimeVect[i] << endl;
            }
            break;
        case 2:
            cout << "Executed Prisoners\n" << endl;
            break;
        case 3:
            cout << "Prison Statistics\n" << endl;
            cout << "Current Money: " << money << endl;
            cout << "Prisoners: " << prisoners << endl;
            break;
    }
}



C:\Users\Chay Hawk\Desktop\Test project\main.cpp||In member function 'void Vars::Game()':|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|117|error: jump to case label|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|101|error: crosses initialization of 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > crimeVect'|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|100|error: crosses initialization of 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > nameVect'|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|120|error: jump to case label|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|101|error: crosses initialization of 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > crimeVect'|
C:\Users\Chay Hawk\Desktop\Test project\main.cpp|100|error: crosses initialization of 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > nameVect'|
||=== Build finished: 6 errors, 0 warnings ===|
You cannot declare variables inside case statements. This results in them being in the scope of every case statement. To limit the scope of the newly declared variable, enclose the case statements in brackets {} to limit the scope to the current case statement.

1
2
3
4
5
6
7
8
9
case 1:
{
    statement;
}break;
case 2:
{
    statement;
    statement;
}break;

etc.
Ok i got it now i have a problem with my vector, its supposed to output 6 DIFFERENT inmate names but it only outputs 1 6 times or so

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
void Vars::Game()
{
    int choice;
    int random;

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

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

    cout << "1) View list of prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison statistics" << endl;
    cout << "4) Execute Prisoner(s)" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            {
                cout << "Inmate list, maximum 25 inmates\n" << endl;

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

                srand(time(0x0));

                random = rand() % 6;

                for(int i = 0; i < random; i++)
                {
                    nameVect.push_back(randNames());
                    crimeVect.push_back(randCrimes());
                }

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

                for(int i = 0; i < random; i++)
                {
                    cout << nameVect[i] << " ";
                    cout << crimeVect[i] << endl;
                }
            }
            break;
        case 2:
            cout << "Executed Prisoners\n" << endl;
            break;
        case 3:
            cout << "Prison Statistics\n" << endl;
            cout << "Current Money: " << money << endl;
            cout << "Prisoners: " << prisoners << endl;
            break;
    }
}
This one I'm not sure about.
Could you possibly set some breakpoints and see if randName() is generating 6 different names and not simply spitting out the same name over and over.
Also, I have had trouble getting subscripting to work with vectors in the past. Try it with iterators and see if that helps at all.
Idk here is my full 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
#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;
};

int main()
{
    int choice;

    Vars v;

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

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

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

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

        cout << "\n";

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

        cout << "\n";

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

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

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

        file.close();

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

        file.open("prison.txt");

        file >> v.playerName;
        file >> v.prisonName;
        file >> v.money;
        file >> v.prisoners;

        file.close();

        v.Game();
    }
}

void Vars::Game()
{
    int choice;
    int random;

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

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

    cout << "1) View list of prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison statistics" << endl;
    cout << "4) Execute Prisoner(s)" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            {
                cout << "Inmate list, maximum 25 inmates\n" << endl;

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

                srand(time(0x0));

                random = rand() % 6;

                for(int i = 0; i < random; i++)
                {
                    nameVect.push_back(randNames());
                    crimeVect.push_back(randCrimes());
                }

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

                for(int i = 0; i < random; i++)
                {
                    cout << nameVect[i] << " ";
                    cout << crimeVect[i] << endl;
                }
            }
            break;
        case 2:
            cout << "Executed Prisoners\n" << endl;
            break;
        case 3:
            cout << "Prison Statistics\n" << endl;
            cout << "Current Money: " << money << endl;
            cout << "Prisoners: " << prisoners << endl;
            break;
    }
}

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

    srand(time(0x0));

    return names[rand() % 17];
}

string Vars::randCrimes()
{
    string crimes[10] =
    {
        "Rape",
        "Child Abuse",
        "Murder",
        "Assault",
        "Breaking and Entering",
        "Domestic Violence",
        "Unlawful Posession of a Firearm",
        "Destruction of private property",
        "Kidnapping",
        "Armed Robbery"
    };

    srand(time(0x0));

    return crimes[rand() % 10];
}
Your problem is srand(). it should only be called once at the beginning of main. Otherwise you will get always the same value when it's called within 1 second which is likely.

No matter calling srand() more than once does more harm than than good.
Ok sweet i got it, now there are some other problems i have been having. when i load my info from my text file:

Chay Hawk
Michigan Correctional
50000
0

It should display the amount of money and prisoners but it says i have 0 money and 99404904234234 prisoners? 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
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
#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;
};

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

    Vars v;

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

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

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

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

        cout << "\n";

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

        cout << "\n";

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

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

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

        file.close();

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

        file.open("prison.txt");

        file >> v.playerName;
        file >> v.prisonName;
        file >> v.money;
        file >> v.prisoners;

        file.close();

        v.Game();
    }
}

void Vars::Game()
{
    int choice;
    int random;

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

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

    cout << "1) View list of prisoners" << endl;
    cout << "2) View list of executed prisoners" << endl;
    cout << "3) View prison statistics" << endl;
    cout << "4) Execute Prisoner(s)" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            {
                cout << "Inmate list, maximum 25 inmates\n" << endl;

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

                random = rand() % 6;

                for(int i = 0; i < random; i++)
                {
                    nameVect.push_back(randNames());
                    crimeVect.push_back(randCrimes());
                }

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

                for(int i = 0; i < random; i++)
                {
                    cout << nameVect[i] << " ";
                    cout << crimeVect[i] << endl;
                }
            }
            break;
        case 2:
            cout << "Executed Prisoners\n" << endl;
            break;
        case 3:
            cout << "Prison Statistics\n" << endl;
            cout << "Current Money: " << money << endl;
            cout << "Prisoners: " << prisoners << endl;
            break;
    }
}

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

    return names[rand() % 17];
}

string Vars::randCrimes()
{
    string crimes[10] =
    {
        "Rape",
        "Child Abuse",
        "Murder",
        "Assault",
        "Breaking and Entering",
        "Domestic Violence",
        "Unlawful Posession of a Firearm",
        "Destruction of private property",
        "Kidnapping",
        "Armed Robbery"
    };

    return crimes[rand() % 10];
}
Last edited on
the problem is the space within the name. the operator>> stops at the first space. you need to use getline() like on line 47
Like this?

file >> (file, v.prisonName);

But that doesnt explain why the money doesnt load and the prisoner count doesnt load, it says i have 432545234423432432 prisoners when it should be 0 and it says i have 0 money when it should be 50000
No, line 71 should look like this and 72 accordingly:

getline(file, v.playerName);

use this whenever you want blanks included
but what about loading the variables in? thats where i have the problem
Write it like so:
1
2
3
4
        getline(file, v.playerName);
        getline(file, v.prisonName);
        file >> v.money;
        file >> v.prisoners;
and all variables are set according to your example.
Last edited on
No I meant in the game function, when I go to the case statement to view money and prisoners, thats where the problem is, or is that what you mean?
you mean line 128/129. Well, you saw the wrong values because you read the wrong values (line 71 to 74). If you read them like I showed you above then you'll see the right values. so line 128/129 are not the culprit
Ok, im sorry but im still not getting it, so when you say im reading the wrong values, do you mean money could be trying to read the players name and thats why i am getting the huge numbers?
bump
money could be trying to read the players name and thats why i am getting the huge numbers?
Yes, kind of.

if you read the data set like you did, following will happen:
1
2
3
4
        file >> v.playerName; // v.playerName = Chay
        file >> v.prisonName; // v.prisonName = Hawk (remember >> stops at whitespace)
        file >> v.money; // v.money -> Error! The value remains unchanged (uninitialzed in your case) because Michigan is not an integer
        file >> v.prisoners;  // file is in error state -> v.prisoners remains what it was 


I suggest to try that out either with debugging or with debug output
Last edited on
ok i got it thanks.
Topic archived. No new replies allowed.