Case - 2 things do the same

Hi,

I have a case statement, but two of the options do the same.

Like this:
case 'n':
*code*
case 'N':
*code*

It can be coded like that, but the code should be written twice, but I only want it once. I've tried to solve it on this way:

case 'n' || 'N':

and

case 'n' || case 'N':

But both doesn't work. How to do this?

Another question... is there an easy way to convert lowercase letters to uppercase or uppercase to lowercase?

Thank you very much :)

EDIT:
I've found out uppercase can be done with "toupper". Any idea about the case question? Thanks
Last edited on
You can "drop thru" like:

1
2
3
4
5
6
7
8
9
10
11
12
switch (ch)
{
case 'n':
case 'N':
    // no code
    break;

case 'y':
case 'Y':
    // yes code
    break;
}
Thank you, kbw =)
Look up toupper() and tolower() for the answer to your other questions.
maybe trying taking the users input and then altering it to a way you can work with: e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << "Please enter [y/n]: ";
cin >> ans;

if(isupper(ans)) ans = tolower(ans); //checks is the input it a capital & if so, makes it lowercase

switch(ans)
{
   case 'y': 
    // code
    break;

   case 'n':
    // code
    break;
}
Last edited on
Topic archived. No new replies allowed.