C++, getting a infinite loop

后端 未结 2 2035
半阙折子戏
半阙折子戏 2021-01-15 20:55

i try to do a simple menu using switch. I also want to do a check if the user made a valid input (Only int from 1 to 4). Entering -4 or 44 is working fine with this check. B

2条回答
  •  死守一世寂寞
    2021-01-15 21:26

    It's because the input is trying to get an integer. When the input is not an integer, the input is left in the buffer, so next time around in the loop the same input is still there.

    Also, you are not calling the flushCin function in the default case, you are declaring it. You might want to remove the void keyword. I guess it does the correct thing? (I.e. calling std::cin.ignore() and std::cin::clear().)

提交回复
热议问题