How come if I enter a letter input my program gets stuck in its while loop?

前端 未结 2 1559
予麋鹿
予麋鹿 2021-01-07 05:03

Sorry if I fail to be clear enough or make any mistakes, this is my first time posting.

My code runs without errors when complied but the first while loop (in in

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-07 05:30

    I am a newbie to programming in general, but playing with your code and looking up stuff made me find some sort of solution. The cin.clear only clears the error log of the input, and I believe that it still retains the value of the letter.

    What you should add right after is a cin.ignore(#,'\n') (# being a very, very large number) to have it avoid the line and skip right through it.

    Found the solution in another question that explains the use of both cin commands.

提交回复
热议问题