Switch statement using or

前端 未结 7 2406
时光说笑
时光说笑 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:46

    if you do

    case('s' || 'S'):
        // some code
    default:
        // some code
    

    both s and S will be ignored and the default code will run whenever you input these characters. So you could decide to use

    case 's':
    case 'S':
        // some code
    

    or

    switch(toupper(choice){
        case 'S':
            // some code.
    

    toupper will need you to include ctype.h.

提交回复
热议问题