0

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?

Ken R.
  • 69
  • 1
  • 6
  • Perhaps you need to use char c; cin.get(c); you'll have to convert it to an integer after – GWW Dec 03 '10 at 03:02
  • I was thinking it was std::string too.. It is hard to accept its use if it does this to me each time. – Ken R. Dec 03 '10 at 03:08

3 Answers3

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);
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • Unfortunately I tried appending that before both of them, and it infinite looped again. :( I even tried before all the `cin`'s. This is so confusing! – Ken R. Dec 03 '10 at 03:20
  • Can you try this '{while ((cin.peek()=='\n')||(cin.peek()=='\r')) cin.get();}' – Chubsdad Dec 03 '10 at 03:30
  • Where do I put it, why are there brackets surrounding it, and do you mean cin instead of fin? Thank you. – Ken R. Dec 03 '10 at 03:33
  • instead of cin.ignore use the code 'while ((cin.peek()=='\n')||(cin.peek()=='\r')) cin.get();' – Chubsdad Dec 03 '10 at 03:37
  • Unfortunately the same thing as well, the infinite loop spits out both cout/cin requests for entering text+shift + the menu infinite times, its like it somehow completely ignoring each `cin` request infinitely, even though nothing was entered... same thing on someone else's compiler! – Ken R. Dec 03 '10 at 03:41
  • THANK YOU!!! I called this cin.ignore + cin.clear and it stopped it completely, the problem was mainly with getline() which was accepting a newline unless I used cin.ignore..:) – Ken R. Dec 03 '10 at 03:58
0

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

    cin >> selection;
    cin.clear(); 
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • Woah... doing this makes it loop on menu 4 times then awaits my selection.. This did something, heh, I'll try to see what it does, this is so weird why this is happening.. – Ken R. Dec 03 '10 at 03:45
  • @Chubsdad is correct it does clear the flags but it seems to stop your loop – Mark Hall Dec 03 '10 at 03:47
  • I believe why it is working is that you are reading a char from cin and assigning it to a integer which is causing the error flag to be set, why not change selection to a char and test for char instead of integers. – Mark Hall Dec 03 '10 at 03:55
  • Thank you, it worked along with Chubsdad's solution :) I wish I could +1. – Ken R. Dec 03 '10 at 03:58
0

This page should explain all you need to know.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153