Switch statement using or

前端 未结 7 2358
时光说笑
时光说笑 2020-12-10 10:29

I\'m creating a console app and using a switch statement to create a simple menu system. User input is in the form of a single character that displays on-screen

7条回答
  •  囚心锁ツ
    2020-12-10 10:42

    Just use tolower(), here's my man:

    SYNOPSIS
    #include ctype.h

       int toupper(int c);
       int tolower(int c);
    

    DESCRIPTION toupper() converts the letter c to upper case, if possible.

       tolower() converts the letter c to lower case, if possible.
    
       If c is not an unsigned char value, or EOF, the behavior of these
       functions is undefined.
    

    RETURN VALUE The value returned is that of the converted letter, or c if the conversion was not possible.

    So in your example you can switch() with:

    switch(tolower(menuChoice)) {
        case('q'):
            // ...
            break;
        case('s'):
            // ...
            break;
    }
    

    Of course you can use both toupper() and tolower(), with capital and non-capital letters.

提交回复
热议问题