473,394 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

cin >> string looping infinitely

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
1 2036
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

6
by: Bengt Richter | last post by:
>>> hex(-5) __main__:1: FutureWarning: hex()/oct() of negative int will return a signed string in Python 2.4 and up '0xfffffffb' >>> hex(-5) '0xfffffffb' >>> hex(-5L) '-0x5L' That is sooo...
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
1
by: @ndy | last post by:
Hellow programmers, does anybody has any suggestions for the next problem? I'm looping a recordset and during the loop it allso calculate some things. Now I want display a POPUP FORM with...
17
by: Peter Bromley | last post by:
The following code snippet does not seem to work correcly unsigned __int64 result = 0xFFFFFFFFFFFFFFFF; result = result << 64; Debugger.WriteLine(System::String::Format(S"{0:X16}",...
3
by: Olaf Rabbachin | last post by:
Hi *, ich erzeuge zur Laufzeit in einer ASP-Tabelle Tabellenzellen. Im Großteil dieser Zellen befinden sich Checkboxes. Per JS ändere ich beim Click auf eine solche Zelle oder deren Checkbox die...
11
by: Ole Nielsby | last post by:
First, sorry if this is off-topic, not strictly being a C++ issue. I could not find a ng on numerics or serialization and I figure this ng is the closest I can get. Now the question: I want...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
12
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.