conditional statement in while loop

前端 未结 2 1998
情歌与酒
情歌与酒 2021-01-25 17:48

I must have missed something. I\'m doing an exercise to learn c++ and it asks that if a user inputs either c,p,t or g character then carry on, otherwise re-request prompt, so I

2条回答
  •  耶瑟儿~
    2021-01-25 18:04

    My understanding is that the "OR" statement should work as one of the tests is correct.

    Well, you could use ||, but the expression would have to be:

    while(!(ch == 'c' || ch == 'p' || ch == 't' || ch == 'g'));
    

    By applying the De Morgan's law, the above simplifies to:

    while(ch != 'c' && ch != 'p' && ch != 't' && ch != 'g');
    

提交回复
热议问题