By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

cin >> string looping infinitely

P: 8
Because of a simple cin statement, my program goes into an infinite loop and I'm not sure why. I've used the same code in other programs (only with different variable names and cout statements) and it's worked fine. See code below:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int main () 
  3. {
  4.     BinaryHeap pQueue; // create empty heap
  5.     bool acceptCommand = true;
  6.     string command; // function to perform
  7.  
  8.     while (acceptCommand)
  9.     {    
  10.         // get function to perform
  11.         cout << "Please enter a command: " ;
  12.         cin >> command ;
  13.         cout << endl ;
  14.  
  15.         // act accordingly
  16.         if (command=="createHeap")
  17.         {
  18.             ...
  19.         }
  20.  
  21.         else if (command=="insert")
  22.         {
  23.             ...
  24.         }
  25.  
  26.         else if (command=="deleteMin")
  27.         {
  28.             ...
  29.         }
  30.  
  31.         else if (command=="deleteHeap")
  32.         {
  33.             ...
  34.         }
  35.  
  36.         else if (command=="remove")
  37.         {
  38.             ...
  39.         }
  40.  
  41.         else if (command=="decreaseKey")
  42.         {
  43.             ...
  44.         }
  45.  
  46.         else if (command=="quit")
  47.         {
  48.             acceptCommand = false;            
  49.         }
  50.  
  51.         else // invalid command
  52.         {
  53.             cout << "Command not recognized. Please try again." ;
  54.         }
  55.  
  56.     } // end while
  57.  
  58.     return 0;
  59. }
  60.  
  61.  
Using a BinaryHeap class and associated functions, the above is supposed to take in a command, act accordingly, and then wait to accept the next instruction. When compiled and run on both XCode and TextMate, the input "createHeap 1 2 3 4 5" will execute the createHeap function correctly once but then attempt to re-execute infinitely. It seems I need to reset the cin variable after the command is given, but cin.clear() and cin.ignore() seem to have no effect. Any suggestions? Thanks in advance.
Apr 27 '08 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
You dpn't check that con >> command actaully worked.

If it fails, the input stream goes into a fail state and the operator>> aborts.

Next time around the operator>> sees the fail state and does nothing.

And there you are.

You need to check your >> statements:
Expand|Select|Wrap|Line Numbers
  1. cin >> command;
  2. if (cin.fail())
  3. {
  4.       //Here you need to a) cin.clear() to reset the fail state and
  5.       //b) get rid of the data causing the problem
  6.       //etc...
  7. }
  8.  
Apr 28 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.