Struct Variables Not Working in switch statement

I did a little debugging and i found that everything in my program works fine until i call this function, the function is supposed to set variables inside the struct to whatever color they need to be but they are coming up empty in cout.

Edit: Did some more debugging and found that p1name comes up as "". (Empty)

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
255
256
257
258
259
260
261
262
struct playerid{
    int p1color;
    int p2color;
    int p3color;
    int p4color;
    int p5color;
    int p6color;
    int p7color;
    int p8color;

    string p1name;
    string p2name;
    string p3name;
    string p4name;
    string p5name;
    string p6name;
    string p7name;
    string p8name;
};

void determinecolors(){
    playerid idkey;
        switch(idkey.p1color){
        case 1:
            idkey.p1name = "Red        ";
            break;
        case 2:
            idkey.p1name = "Blue       ";
            break;
        case 3:
            idkey.p1name = "Teal       ";
            break;
        case 4:
            idkey.p1name = "Purple     ";
            break;
        case 5:
            idkey.p1name = "Yellow     ";
            break;
        case 6:
            idkey.p1name = "Orange     ";
            break;
        case 7:
            idkey.p1name = "Green      ";
            break;
        case 8:
            idkey.p1name = "Light Pink ";
            break;
        default:
            idkey.p1name = "**No Player";
            break;
        }

        switch(idkey.p2color){
        case 1:
            idkey.p2name = "Red        ";
            break;
        case 2:
            idkey.p2name = "Blue       ";
            break;
        case 3:
            idkey.p2name = "Teal       ";
            break;
        case 4:
            idkey.p2name = "Purple     ";
            break;
        case 5:
            idkey.p2name = "Yellow     ";
            break;
        case 6:
            idkey.p2name = "Orange     ";
            break;
        case 7:
            idkey.p2name = "Green      ";
            break;
        case 8:
            idkey.p2name = "Light Pink ";
            break;
        default:
            idkey.p2name = "**No Player";
            break;
        }

        switch(idkey.p3color){
        case 1:
            idkey.p3name = "Red        ";
            break;
        case 2:
            idkey.p3name = "Blue       ";
            break;
        case 3:
            idkey.p3name = "Teal       ";
            break;
        case 4:
            idkey.p3name = "Purple     ";
            break;
        case 5:
            idkey.p3name = "Yellow     ";
            break;
        case 6:
            idkey.p3name = "Orange     ";
            break;
        case 7:
            idkey.p3name = "Green      ";
            break;
        case 8:
            idkey.p3name = "Light Pink ";
            break;
        default:
            idkey.p3name = "**No Player";
            break;
        }

        switch(idkey.p4color){
        case 1:
            idkey.p4name = "Red        ";
            break;
        case 2:
            idkey.p4name = "Blue       ";
            break;
        case 3:
            idkey.p4name = "Teal       ";
            break;
        case 4:
            idkey.p4name = "Purple     ";
            break;
        case 5:
            idkey.p4name = "Yellow     ";
            break;
        case 6:
            idkey.p4name = "Orange     ";
            break;
        case 7:
            idkey.p4name = "Green      ";
            break;
        case 8:
            idkey.p4name = "Light Pink ";
            break;
        default:
            idkey.p4name = "**No Player";
            break;
        }

        switch(idkey.p5color){
        case 1:
            idkey.p5name = "Red        ";
            break;
        case 2:
            idkey.p5name = "Blue       ";
            break;
        case 3:
            idkey.p5name = "Teal       ";
            break;
        case 4:
            idkey.p5name = "Purple     ";
            break;
        case 5:
            idkey.p5name = "Yellow     ";
            break;
        case 6:
            idkey.p5name = "Orange     ";
            break;
        case 7:
            idkey.p5name = "Green      ";
            break;
        case 8:
            idkey.p5name = "Light Pink ";
            break;
        default:
            idkey.p5name = "**No Player";
            break;
        }

        switch(idkey.p6color){
        case 1:
            idkey.p6name = "Red        ";
            break;
        case 2:
            idkey.p6name = "Blue       ";
            break;
        case 3:
            idkey.p6name = "Teal       ";
            break;
        case 4:
            idkey.p6name = "Purple     ";
            break;
        case 5:
            idkey.p6name = "Yellow     ";
            break;
        case 6:
            idkey.p6name = "Orange     ";
            break;
        case 7:
            idkey.p6name = "Green      ";
            break;
        case 8:
            idkey.p6name = "Light Pink ";
            break;
        default:
            idkey.p6name = "**No Player";
            break;
        }

        switch(idkey.p7color){
        case 1:
            idkey.p7name = "Red        ";
            break;
        case 2:
            idkey.p7name = "Blue       ";
            break;
        case 3:
            idkey.p7name = "Teal       ";
            break;
        case 4:
            idkey.p7name = "Purple     ";
            break;
        case 5:
            idkey.p7name = "Yellow     ";
            break;
        case 6:
            idkey.p7name = "Orange     ";
            break;
        case 7:
            idkey.p7name = "Green      ";
            break;
        case 8:
            idkey.p7name = "Light Pink ";
            break;
        default:
            idkey.p7name = "**No Player";
            break;
        }

        switch(idkey.p8color){
        case 1:
            idkey.p8name = "Red        ";
            break;
        case 2:
            idkey.p8name = "Blue       ";
            break;
        case 3:
            idkey.p8name = "Teal       ";
            break;
        case 4:
            idkey.p8name = "Purple     ";
            break;
        case 5:
            idkey.p8name = "Yellow     ";
            break;
        case 6:
            idkey.p8name = "Orange     ";
            break;
        case 7:
            idkey.p8name = "Green      ";
            break;
        case 8:
            idkey.p8name = "Light Pink ";
            break;
        default:
            idkey.p8name = "**No Player";
            break;
        }
}
Last edited on
In the determinecolors function you create a new playerid object. The member variables p1color, p2color, p3color, etc. will be uninitialized when you try to use them in the switches. When the function ends the object is destroyed.

The function seems pointless as it is. Did you mean to pass the object as argument to the function and/or return the object from the function, perhaps?
Last edited on
You're not showing us the cout, which I assume is in your main function and not in determinecolors. If that's the case, the problem is line 22. Your playid struct is local within determine colors. That variable goes out of scope and changes to it are lost when the function exits. You need to pass in the playerid struct from main as an argument by reference.

BTW, Your really shoud treat playerid as an array. You're repeating essentially the same code 8 times. Consider this:
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
struct playerid
{ int pcolor;
   string pname;
};

void determinecolors (playerid & idkey)  // pass in one instance
{  switch (idkey.pcolor)
   {  case 1:
            idkey.pname = "Red        ";
            break;
        case 2:
            idkey.pname = "Blue       ";
            break;
        case 3:
            idkey.pname = "Teal       ";
            break;
        case 4:
            idkey.pname = "Purple     ";
            break;
        case 5:
            idkey.pname = "Yellow     ";
            break;
        case 6:
            idkey.pname = "Orange     ";
            break;
        case 7:
            idkey.pname = "Green      ";
            break;
        case 8:
            idkey.pname = "Light Pink ";
            break;
        default:
            idkey.pname = "**No Player";
            break;
        }
}

int main ()
{ playerid players[8];

  for (i=0;i<8;i++)
    determinecolor(player[i]);
}






Ok got it, i placed my playerid object after the struct playerid.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct playerid{
    int p1color;
    int p2color;
    int p3color;
    int p4color;
    int p5color;
    int p6color;
    int p7color;
    int p8color;

    string p1name;
    string p2name;
    string p3name;
    string p4name;
    string p5name;
    string p6name;
    string p7name;
    string p8name;
};
playerid idkey;

And before i had just the entire contents of the determinecolor function inside my main function but instead was using int's declared inside main instead of a struct. Really i did it to try to organize better rather than have 500 lines of code all inside my main().
Last edited on
Topic archived. No new replies allowed.