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.
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.)
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.
#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.