Why is this cin reading jammed?

Cin.clear() doesn't clear the standard input. What it does is clearing error bits, like eofbit failbit and others, and sets the stream into a good state. Maybe you expected it to clear out anything in it?

If the user typed.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

I've singled out a failure on my program that prevents me from assigning a value to the variable addAntonymAnswer1. I've tried running cin.clear() before the statement to get the thing read my yes/no answer, but the code just won't respond. The program bit that's failing is located inside void dictionaryMenu(vector &wordInfoVector) and reads cin.clear(); cout"; cin>>addAntonymAnswer1; // cin reading STUCK, why!

? To get to that point of the program the user has to choose to add a word, and then add a synonym. The input for running the program is: dictionary.

Txt 1 cute 2 hello 3 ugly 4 easy 5 difficult 6 tired 7 beautiful synonyms 1 7 7 1 3 2 antonyms 1 3 3 1 7 4 5 5 4 7 3 #include #include #include #include #include using namespace std; class WordInfo{ public: WordInfo(){} WordInfo(string newWord){ word=newWord; } ~WordInfo() { } int id() const {return myId;} void readWords(istream &in) { in>>myId>>word; } vector & getSynonyms () { return mySynonyms; } vector & getAntonyms() { return myAntonyms; } string getWord() { return word; } void dictionaryMenu (vector &wordInfoVector){ cout"; string addWordAnswer; cin>>addWordAnswer; if (addWordAnswer=="yes") // case if the guy wants to add a word { cout"; cin>>newWord; cout"; cin>>addSynonymAnswer; if (addSynonymAnswer=="yes") { cout"; cin>>lineOfSyns; newWordInfo. PushSynonyms(lineOfSyns, wordInfoVector); cin.clear(); cout"; cin>>addAntonymAnswer1; // cin reading STUCK, why!? If (addAntonymAnswer1=="yes"){ } else if (addAntonymAnswer1=="no"){ // END DICTIONARY MENU } } else if (addSynonymAnswer=="no"){ cout"; cin>>addAntonymAnswer2; if (addAntonymAnswer2=="yes"){ } else if (addAntonymAnswer2=="no"){ // END DICTIONARY MENU } } } // if addWordAnswer == "no" else if (addWordAnswer=="no"){ // ######RETURN TO MAIN MENU############ } } void pushSynonyms (string synline, vector &wordInfoVector){ stringstream synstream(synline); vector synsAux; // synsAux tiene la línea de sinónimos int num; while (synstream >> num) {synsAux.

Push_back(num);} int wordInfoVectorIndex; int synsAuxCopyIndex; if (synsAux.size()>=2){ // takes away the runtime Error for (wordInfoVectorIndex=0; wordInfoVectorIndex =2 } // end pushSynonyms void pushAntonyms (string antline, vector &wordInfoVector) { stringstream antstream(antline); vector antsAux; int num; while (antstream >> num) antsAux. Push_back(num); int wordInfoVectorIndex; int antsAuxCopyIndex; if (antsAux.size()>=2){ // takes away the runtime Error for (wordInfoVectorIndex=0; wordInfoVectorIndex =2 } //--dictionary output function void printWords (ostream &out) { out (const WordInfo &otherWordInfo)const {return word>otherWordInfo. Word;} public: vector mySynonyms; vector myAntonyms; string word; int myId; }; //--Definition of input operator for WordInfo istream & operator >>(istream &in, WordInfo &word) { word.

ReadWords(in); } //--Definition of output operator ostream & operator wordInfoVector; WordInfo aword; while (inStream >>aword && (!(aword=="synonyms"))) { wordInfoVector. Push_back(aword); } inStream.clear(); vector intVector; string synLine; while (getline(inStream, synLine)&&(synLine! =("antonyms"))){ aword.

PushSynonyms(synLine, wordInfoVector); } int theIndex; string antLine; while (getline(inStream,antLine)){ aword. PushAntonyms(antLine, wordInfoVector); } cout DictionaryMenu(wordInfoVector); system("PAUSE"); return 0; } c++ cin link|improve this question edited Jan 25 '09 at 21:03 asked Jan 25 '09 at 20:51omgzor3,18012378 81% accept rate.

Cin.clear() doesn't clear the standard input. What it does is clearing error bits, like eofbit, failbit and others, and sets the stream into a good state. Maybe you expected it to clear out anything in it?

If the user typed yes no Just before, and you cin >> someStringVariable; It will read up to no and the stream will still contain no The call to clear then clears any error bits being active. Then, your cin>>addAntonymAnswer1; Will read the no that wasn't eaten by the previous read, and the action immediately returns, not waiting for new input. What you should do is doing a clear followed by an ignore, up to the next newline.

You tell it the amount of characters it should ignore maximally. That amount should be the highest number possible: cin.clear(); cin. Ignore(std::numeric_limits::max(), '\n'); Doing that will make the stream empty, and a following read will wait for you to type something in.

Another problem arises if you have got a cin >> followed by a getline: The cin will leave any whitespace (also newlines) after its read token, but getline will stop reading after it hits such a newline. I see you have put clear after nearly everything. So I want to show you when you need it and when not.

You don't need it when you sequence multiple cin >>. Assume you have in your buffer: "foo\nbar\n". Then you do the following reads cin >> a; // 1 cin >> b; // 2 After the first, your buffer will contain "\nbar\n".

That is, the newline is still in. The second cin>> will first skip all whitespace and newlines, so that it can cope with \n being at the front of bar. Now, you can also sequence multiple getline calls: getline(cin, a); getline(cin, b); Getline will throw away the \n that it reads at the line end, but won't ignore newlines or whitespace at the begin.

So, after the first getline, the buffer contains "bar\n". The second getline will correctly read "bar\n" too. Now, let's consider the case where you need the clear/ignore: cin >> a; getline(cin, b); The first will leave the stream as "\nbar\n".

The getline then will see immediately the \n at the begin, and will think it read an empty line. Thus, it will immediately continue and not wait for anything, leaving the stream as "bar\n". So, if you have a getline after a cin>> you should first execute the clear/ignore sequence, to clear out the newline.

But between getline or cin>>'s, you should not do it.

Cin >> ... reads from standard input until it finds a whitespace character. When you enter, say, 8 5 for the list of synonyms, 8 gets read into lineOfSyns and nothing more. When the program reaches cin >> addAntonymAnswer1, 5 gets read into addAntonymsAnswer1.

Your program behaves unexpectedly since it expects yes or no but it got 5. Look at using cin.getline() instead of >>. See, for example, sections 18.2 and 18.3 of this page.

Thanks, I'm using getline now due to your suggestion. – omgzor Jan 25 '09 at 21:46.

In your program you ask the user: Please write on a single line the ids for the synonyms of test starting with its id, which is 8 For example, to define that the synonym of the word 'cute', which has an id 1, I s'beautiful', which has an id 7, you should write: 1 7 In the case of test you should start with 8 Then you attempt to read in the line the user typed with cin>>lineOfSyns; However, this only reads up to the first whitespace. So the 2nd number typed in by the user is still in the cin buffer when the cin>>addAntonymAnswer1; line is executed, so that data gets read into the addAntonymAnswer1 string. The user never gets an opportunity to type in 'yes' or 'no' and your tests for those values fail.

You should consider changing to using the string overload for getline() to do your reads: getline( cin, stringvar); This would probably be better than using cin.getline(), as that does not have an overload that takes string - with that member function, you need to read into an array of chars, which is far less flexible than reading into a string.

Thanks, this was helpful. – omgzor Jan 25 '09 at 21:46 You should consider changing to using the string overload for getline() to do your reads: getline( cin, stringvar); – Michael Burr Jan 25 '09 at 21:55 Sure, I already did. – omgzor Jan 26 '09 at 0:07.

It is "stuck" because it is waiting for input. Cin is attached to the standard input handle for the program, you have to type something and hit enter.

I mean "stuck"in the sense that it doesn't let me "type something and hit enter". The console inmediately presents "Press any key to continue". – omgzor Jan 25 '09 at 20:58 I don't think he deserves a downvote.

"getting stuck" means it doesn't continue, which I think he correctly interpreted. Instead, maybe change the wording in your question. – Johannes Schaub - litb Jan 25 '09 at 21:28 it assumes the poster doesn't know that "cin s attached to the standard input handle for the program, you have to type something and hit enter", which I find borderline offensive, the guy didn't even read my code.

– omgzor Jan 25 '09 at 21:39 I read your code. My answer seemed the most likely based on what I could see. You should explain your problem a little better – 1800 INFORMATION Jan 25 '09 at 0:21.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions