Switch Case Loop

I'm trying to make a loop in a switch for each case.
1
2
3
4
5
int itemMenu;

switch (itemMenu){
        for (int x=0; x<10; x++){
                case x :


Where 'x' is after the For Loop, is the number in the case along with the array in each case.
I would put the whole switch in, but it's longer than I had expected with smaller loops (error correction, you just got to love them) and switches

When I compile the program, I get the error:
error C2051: case expression not constant
The error line is referring to "case x :" in the code.

My question is, is what I am doing even possible, if so, what am I doing wrong?
Why is the loop before the case? You can't do that. The case has to follow a switch. That's the point. What you are trying to do is a syntax error, plain and simple. You can't loop a case, any more than you can loop a condition check in an if statement.
Try putting the loop outside the switch altogether. That should probably still do what you want.
Last edited on
Right.....you can just label the case values...

1
2
3
4
5
6
7
8
9
switch(itemMenu)
{
case 1:
// code
break;
case 2:
// code
break;
}
Thanks for the replies.
I was trying to make a compact piece of code that accessed an array to edit parts of it by choice while letting room for expansion.

This is what I tried to do (some parts may not work because I modified to a new method, and tried to revert back to show what I tried.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
switch (itemMenu){
	for (int x=0; x<10; x++){ 
		case x :
			int complete = 0;
			do {
				system ("cls");
				int x = itemMenu - 1;
				cout << "Current Item Is: " << colorArray[x][0] << endl;
				cout << "Are you sure you want to edit the name of: " << colorArray[x][0] << "? Y/N" << endl;
				cin >> correction1;
				correction1 = toupper(correction1);
				if (correction1 == 'Y'){
					cout << "The new name for: " << colorArray[x][0] << " is: ";
					cin >> newName;
					itemArray[x][0] = newName;
					complete = 1;
				}if (correction1 != 'N' && correction1 != 'Y'){
				cout << "Incorrect Answer. Please input 'Y' (Yes) or 'N' (No)." << endl;
				}
			}while (complete == 0);
			break;
		}


I currently got the code working through "if" statements with greater and lesser statements for the options and "x = itemMenu" for the array. It works if anyone is interested in seeing.
This won't work due to the reasons tummychow and yoked88 mentioned above.

The for loop can be simply moved outside the switch statement but in any case the case value has to be a constant not a variable.

either
1
2
switch (itemMenu){
case 0: /* code */

or
1
2
3
const int x=0;
switch (itemMenu){
case x: /* code */
Rather you could do this: using if instead of switch.

1
2
3
4
5
6
7
for(int i=0; i<10; i++)
{
  if(itemMenu == i)
  {
    //do the something
  }
}


I hope this would work.
That's what I did. I figured after a while that the cases had to constants, but wasn't 100% sure. So I asked to see if there was a possible way.

This is what I got working. It does what I need, though still needs some minor tweaking.
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

#include "header.h"
void admin ()
{
	char menu1;							// Beginning Menu Switch
	char styleList;						// Style List Option for editing Names
	int itemMenu;						// Which of the items in the menu to edit
	string newName;						// User input for new name of selected item
	char correction1;
	string temp;
	string colorArray[10][3];
	string itemArray[10][3];


	ifstream inSW;					// Create an input file stream.
	inSW.open("Southwest.txt");		// Use it to read from a file named data.txt.
	for (int x=0; x<10; x++) {
		getline(inSW, colorArray[x][0]);		// Read the first item from the file into an integer variable x.
	}
	inSW.close();

	ifstream inS;					// Create an input file stream.
	inS.open("South.txt");			// Use it to read from a file named data.txt.
	for (int x=0; x<10; x++) {
		getline(inS, itemArray[x][0]);		// Read the first item from the file into an integer variable x.
	}
	inS.close();

	do {
	system ("cls");
	cout << "\nYou are now in Administration Mode" << endl << endl;

	cout << "<=================>" << endl;
	cout << " A. Edit Item List" << endl;
	cout << "<=================>\n" << endl;
	cin >> menu1;
	menu1 = toupper(menu1);

	
	switch (menu1) {
		case 'A' :
			do {
			system ("cls");
			cout << "Which of the following would you like to edit?\n" << endl; 
			cout << "A  South\nB  Southwest\nC  Done" << endl;
			cin >> styleList;
			styleList = toupper(styleList);
			
				temp = "nDone";
				switch (styleList){
					case 'A' :
						do{
							system ("cls");
							cout << "Which of the following would you like to edit in South?" << endl;
							cout << "<====================>" << endl;
							int y=1;
							for (int x=0; x<10; x++) {
								cout << y << "  " << itemArray[x][0] << endl;
								y++;
							}
							cout << "11  Done and Save" << endl;
							cout << "12  Exit and DON'T Save" << endl;

							cin >> itemMenu;
							if (itemMenu >= 1 && itemMenu <= 10){
								system ("cls");
										int complete = 0;
										do {
											system ("cls");
											int x = itemMenu - 1;
											cout << "Current Item Is: " << itemArray[x][0] << endl;
											cout << "Are you sure you want to edit the name of: " << itemArray[x][0] << "? Y/N" << endl;
											cin >> correction1;
											correction1 = toupper(correction1);
											if (correction1 == 'Y'){
												cout << "The new name for: " << itemArray[x][0] << " is: ";
												cin >> newName;
												//getline(cin, newName);
												itemArray[x][0] = newName;
												complete = 1;
											}if (correction1 != 'N' && correction1 != 'Y'){
												cout << "Incorrect Answer. Please input 'Y' (Yes) or 'N' (No)." << endl;
											}
										}while (complete == 0);
							}
						if (itemMenu == 11){
							system ("cls");
								ofstream inS;
								inS.open("South.txt");
								for (int x=0; x<10; x++) {
									inS << itemArray[x][0] << endl;
									cout << itemArray[x][0] << " Saved name." << endl;
								}
								inS.close();
								temp = "done";
							}if (itemMenu == 12){
								temp = "done";
							
							}
						styleList = 'Z';
						}while (temp != "done");
						break;
					case 'B' :
						do{
							system ("cls");
							cout << "Which of the following would you like to edit in Southwest?" << endl;
							cout << "<====================>" << endl;
							int y=1;
							for (int x=0; x<10; x++) {
								cout << y << "  " << colorArray[x][0] << endl;
								y++;
							}
							cout << "11  Done and Save" << endl;
							cout << "12  Exit and DON'T Save" << endl;

							cin >> itemMenu;
							if (itemMenu >= 1 && itemMenu <= 10){
								system ("cls");
										int complete = 0;
										do {
											system ("cls");
											int x = itemMenu - 1;
											cout << "Current Item Is: " << colorArray[x][0] << endl;
											cout << "Are you sure you want to edit the name of: " << colorArray[x][0] << "? Y/N" << endl;
											cin >> correction1;
											correction1 = toupper(correction1);
											if (correction1 == 'Y'){
												cout << "The new name for: " << colorArray[x][0] << " is: ";
												cin >> newName;
												//getline(cin, newName);
												itemArray[x][0] = newName;
												complete = 1;
											}if (correction1 != 'N' && correction1 != 'Y'){
												cout << "Incorrect Answer. Please input 'Y' (Yes) or 'N' (No)." << endl;
											}
										}while (complete == 0);
							}
						if (itemMenu == 11){
							system ("cls");
								ofstream inSW;
								inSW.open("Southwest.txt");
								for (int x=0; x<10; x++) {
									inSW << colorArray[x][0] << endl;
									cout << colorArray[x][0] << " Saved name." << endl;
								}
								inSW.close();
								temp = "done";
							}if (itemMenu == 12){
								temp = "done";
							}
						styleList = 'Z';
						}while (temp != "done");
						break;
					case 'C' :
						menu1 = 'Z';
						break;
					default :
						cout << "Incorrect Input Character." << endl;
				}
			}while (styleList != 'A' && styleList != 'B' && styleList != 'C');
			
			break;
		case 'B' :
				
				menu1 = 'Z';
				break;
		default : cout << "Incorrect Input Character." << endl;
	}
}while (menu1 != 'A' && menu1 != 'B');


Only issue I have for now is my getlines do not work, the program skips them and leaves the selected array item (basically the option name) empty. Any ideas there would be helpful, but I will figure it out soon enough. For now, I used a simple cin << newName work through for now.
Topic archived. No new replies allowed.