0

So basically, I'm creating a switch statement that if the user enters a p or if they enter a P it will still work, any ideas? Thank you

4
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by grumpier
0
switch (choice) 
{
 case 'p' || 'P': // do this
 break;
case 's' || 'S': // do that
}

won't work

0

Convert the string to evaluate to lower case, then use it.
I prefer if-else statments, of stings for input.

For ASCII
In a loop of string size:

if (is upper case(91< && 64>)) then
    swap_case()

Swap function:

function swap_case(&character)
    character XOR 32
0

Or for a more reliable solution (ei for more than just ASCII) you can use tolower() and toupper() functions found in the cctype header

Chris

0
switch (choice) 
{
 case 'p' || 'P': // do this
 break;
case 's' || 'S': // do that
}

won't work

Indeed. 'p' || 'P' would expand to a bool value of true, as will 's' || 'S'. Compilers will generally refuse to compile the above, as particular cases of a switch statement are not allowed to be duplicated.

Try this instead;

switch (choice) 
{
 case 'p':
 case 'P': // do this
 break;
case 's':
case 'S': // do that
  break;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.