I’m getting a few errors in my main when I am using a while loop and a switch statement. I have checked the curly braces and I can’t spot the typo. Can anybody see the problem?
bool menu = true;
while(menu)// Loop to revert back to menu when choice is not compatable with options.
{
int selection;
cout<< "Choice: ";
cin>> selection;
switch(selection)
{
case 1:
cout<< "View Elements.";
Array.print();
break;
case 2:
cout<< "Delete Element. ";
break;
case 3:
cout<< "Delete All Elements. ";
//Array.clear(OrderedArray);
break;
case 4:
cout<< "Insert Element. ";
//Array.Insert();
break;
case 5:
cout<< "Search for Element. ";
bool searchMenu = true;
while(searchMenu)
{
int searchSelection;
cout<< "Choice: ";
cin>> searchSelection;
switch(searchSelection)
{
case 1:
cout<< "Linear Search. ";
//Array.linearSearch();
break;
case 2:
cout<< "Binary Search ";
//Array.binarySearch();
break;
}
}
break;
case 6:
cout<< "Store. ";
bool storeMenu = true;
while(storeMenu)
{
int storeSelection;
cout<< "Choice: ";
cin>> storeSelection;
switch(storeSelection)
{
case 1:
cout<< "Write File ";
//Array.writeFile();
break;
case 2:
cout<< "Read File ";
//Array.readFile();
break;
}
}
break;
case 7:
cout<< "Sort. ";
Array.sort();
break;
case 8:
cout << "Exit.";
cout << "Please press Enter to exit.";
}
}
These are the errors:
error C2360: initialization of 'searchMenu' is skipped by 'case' label c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp 71 1 Repeat - Ordered Array
error C2360: initialization of 'storeMenu' is skipped by 'case' label c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp 94 1 Repeat - Ordered Array
error C2360: initialization of 'searchMenu' is skipped by 'case' label c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp 94 1 Repeat - Ordered Array
error C2360: initialization of 'storeMenu' is skipped by 'case' label c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp 98 1 Repeat - Ordered Array
error C2360: initialization of 'searchMenu' is skipped by 'case' label c:\users\conor\documents\college\c++\projects\repeat - ordered array\repeat - ordered array\orderedarray.cpp 98 1 Repeat - Ordered Array`
Once the code for a case statement gets to be longer than two or three lines, it should probably be turned into a separate function. Especially with these nested menus. The other answers are correct that the compiler is complaining about defining variables in case statements; code reviewers will complain that the switch statement is too complex.