I was just wondering if it was possible to have a variable case using the keyboard? So the cases can be any choice from a to z and its completely variable eg: only choices a),b) and c) appear in one condition bur in another a),b),c),d) and e) might appear. Assume they all call the same function but input a different argument.
char choice;
int i;
while(true)
{
// This increments from char 'a' onward to 'b', 'c', 'd', etc.
for (i = 0; i < number; i++){
char character = 'a';
cout << static_cast<char>(character + i) << ") list of choices" << endl;
}
cin >> choice;
switch(choice) {
// Same incremental principal to achieve variable cases
for (i = 0; i < number; i++){
char character = 'a';
character = (static_cast<char>(character + i) );
case character: carryOutJob(argumentDependantOnCharacter);
}
}
}
If I run code similar to this I get errors that look a little something like this:
: error: ‘character’ cannot appear in a constant-expression
: error: jump to case label [-fpermissive]
: error: crosses initialization of ‘char character’
: error: expected ‘;’ before ‘}’ token
Error 1
C++ rule say
caseexpression must be constant, so compiler can create code for it at compile time, and beside that if you are checking one character at a time why you don’t useif?