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

infinate loop error with char input

P: 2
Here is a block of code that I am having trouble with. I rewrote it as a main function, so I could isolate the problem to fix it, but still haven't been able to figure out the problem. I am pretty new to C++ still.

As I understood it, when cin is used to get text, it would cut off the text at the first space. That's what I was expecting, but in this code, if you enter two words at once - like "red hat" for example - it will go through the loop cycle and never end, ignoring the cin commands inside the loop. Why are those cin commands ignored when extra text is entered? How can I avoid this?

Thank you all for your help.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. /**********************************************************************
  7. * This Function lets you choose one or multiple words.
  8. ***********************************************************************/
  9. int main()//inf. loop error if 2 words entered at once
  10. {
  11.     char word[256];
  12.  
  13.     int choice = 3;// I kept getting 2359208 as my variable.
  14.     //I initialized choice to 3 in an attempt to bump it out of the 
  15.     //loop at the switch and force to choose a new word.
  16.  
  17.     //select word
  18.     cout << "Please type a word.\n";
  19.     cin  >> word;
  20.  
  21.     //modify or finish
  22.     do{
  23.        cout << "Is " << word << " correct?\n"
  24.             << "1 - yes\n"
  25.             << "2 - add more\n"
  26.             << "3 - start over\n";
  27.        cin  >> choice;
  28.  
  29.        //only added for debugging.
  30.        cout << "Your choice is " << choice << endl;
  31.  
  32.        switch (choice)
  33.        {
  34.           case 1://word ok, continue. Needed to add case to avoid default case.
  35.              break;
  36.           case 2://add a space and an additional word
  37.           {
  38.              char wordTemp[256];
  39.              cout << "Enter the additional word that you would like to add.\n";
  40.              cin  >> wordTemp;
  41.  
  42.              if ((strlen(word) + strlen(wordTemp)) < 254)
  43.              {
  44.                 strcat (word, " ");
  45.                 strcat (word, wordTemp);
  46.              }
  47.  
  48.              else//word combo too large. Do not combine
  49.                 cout << "Sorry, those words are too long.\n";
  50.  
  51.              break;
  52.           }
  53.           case 3://choose a new word
  54.           {
  55.              cout << "Please re-enter your word.\n";
  56.              cin  >> word;
  57.              break;
  58.           }
  59.           default://invalid input
  60.           {
  61.              cout << "Incorect response. Please select again.\n";
  62.              cin >> choice;
  63.              break;
  64.           }   
  65.        }
  66.     }while (choice != 1);
  67.  
  68.     cout << "You have selected:\n\"" << word << "\"\n";
  69.  
  70.     cout << "press any key and select enter to finish.\n";
  71.     cin >> choice;
  72.  
  73. return 0;
  74. }
  75.  
  76.  
Jan 23 '08 #1
Share this Question
Share on Google+
5 Replies


gpraghuram
Expert 100+
P: 1,275
Hi,
The problem is the standard input is having unwanted characters which ur second cin>> is reading due to which you get infinite loop.
So you want to clear the standard input buffer before reading again.
You can use this code to clear the unwanted characters

Expand|Select|Wrap|Line Numbers
  1. do{
  2.     char junk[100]; 
  3.     cin.getline(junk,100); //this will remove unwanted characters
  4.  
  5.        cout << "Is " << word << " correct?\n"
  6.             << "1 - yes\n"
  7.             << "2 - add more\n"
  8.             << "3 - start over\n";
  9.        cin  >> choice;
  10.  
  11.  
Thanks
Raghuram
Jan 23 '08 #2

P: 2
Thank you so much for the help!
Jan 23 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
do{
char junk[100];
cin.getline(junk,100); //this will remove unwanted characters

cout << "Is " << word << " correct?\n"
<< "1 - yes\n"
<< "2 - add more\n"
<< "3 - start over\n";
cin >> choice;
This removes only 100 characters. Why 100? Why not 50 or 10000??

To flush the input buffer, write function that will get characters individually until EOF is endountered.
Jan 23 '08 #4

gpraghuram
Expert 100+
P: 1,275
This removes only 100 characters. Why 100? Why not 50 or 10000??

To flush the input buffer, write function that will get characters individually until EOF is endountered.
Whatever i have suggested is only an idea and can be changed by the person who has asked it.
But yours is a good idea to read till EOF


Raghuram
Jan 24 '08 #5

Ganon11
Expert 2.5K+
P: 3,652
Rather than creating an extra variable to read into, you can just use cin.ignore(100, '\n'); which will discard either 100 characters or all characters until the newline character '\n' is encountered, whichever comes first. This runs into the same problem that weaknessforcats described, however.
Jan 24 '08 #6

Post your reply

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