C++: Infinite loop with a simple menu selection

旧时模样 提交于 2019-12-04 05:48:54

问题


The script I am working on is over a page long, so I am going to link it (one simple file):
http://pastebin.com/7BVHmQGp

I apologize for that. My problem is I get into an infinite loop in my code, for example after I select 1 or 2 for encrypting/unencrypting it lets me enter the word, and when I next enter the "shift" for the cipher it runs an infinite loop of the menu.

I had tried for so many hours to debug this, I thought it was a problem with cin, for example when you enter an invalid choice it just throws an infinite loop/

What seems to cause the infininte loops?


回答1:


I think you should be ignoring the newline character instead of a space

I tried with the following and it works on VS2010

    cin.ignore(1, '\n');
    getline(cin, input);



回答2:


Try cin.clear, I believe you are constantly reading your initial input character.

    cin >> selection;
    cin.clear(); 



回答3:


This page should explain all you need to know.



来源:https://stackoverflow.com/questions/4342068/c-infinite-loop-with-a-simple-menu-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!