Perché questa lettura del cin è bloccata?

Ho individuato un errore nel mio programma che mi impedisce di assegnare un valore alla variabile addAntonymAnswer1 . Ho provato a eseguire cin.clear() prima cin.clear() per far yes/no che la cosa legga la mia yes/no , ma il codice semplicemente non risponderà.

Il bit del programma che sta fallendo si trova all’interno di void dictionaryMenu(vector &wordInfoVector) e legge

  cin.clear(); cout<"; cin>>addAntonymAnswer1; // cin reading STUCK, why!? 

per arrivare a quel punto del programma l’utente deve scegliere di aggiungere una parola e quindi aggiungere un sinonimo.

L’input per l’esecuzione del programma è:

 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<<endl<<"Would you like to add a word?"<<endl; cout<<"(yes/no)"<<endl; cout<"; string addWordAnswer; cin>>addWordAnswer; if (addWordAnswer=="yes") // case if the guy wants to add a word { cout<<endl; cout<<"Please, write the word "<<endl; string newWord; cout<"; cin>>newWord; cout<<endl; WordInfo newWordInfo (newWord); int newWordId = wordInfoVector.size() +1; newWordInfo.myId=newWordId; cout<<"The id of "<<newWordInfo.word<<" is "<<newWordInfo.myId<<endl<<endl; wordInfoVector.push_back(newWordInfo); cout<<"Would you like to define which words on the existing dictionary are" <<endl <<"synonyms of "<<newWordInfo.word<<"?"<<endl; cout<<"(yes/no)"<<endl; string addSynonymAnswer, addAntonymAnswer1, addAntonymAnswer2; cout<"; cin>>addSynonymAnswer; if (addSynonymAnswer=="yes") { cout<<endl; cout<<"Please write on a single line the ids for the synonyms of " <<newWordInfo.word<<endl<<"starting with its id, which is "<<newWordInfo.myId<<endl<<endl; cout<<"For example, to define that the synonym of the word 'cute', which has an id 1, is" <<"'beautiful', which has an id 7, you should write: 1 7"<<endl<<endl; cout<<"In the case of "<<newWordInfo.word<<" you should start with "<<newWordInfo.myId<<endl; cin.clear(); string lineOfSyns; cout<"; cin>>lineOfSyns; newWordInfo.pushSynonyms(lineOfSyns, wordInfoVector); cin.clear(); cout<<"Would you like to define which words on the existing dictionary are" <<endl <<"antonyms of "<<newWordInfo.word<<"?"<<endl; //##HERE THE CIN READING OF addAntonymAnswer1 FAILS, WHY? cin.clear(); cout<"; cin>>addAntonymAnswer1; // cin reading STUCK, why!? if (addAntonymAnswer1=="yes"){ } else if (addAntonymAnswer1=="no"){ // END DICTIONARY MENU } } else if (addSynonymAnswer=="no"){ cout<<"Would you like to define which words on the existing dictionary are" <<endl <<"antonyms of "<<newWordInfo.word<<"?"<<endl; 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 <wordInfoVector.size(); wordInfoVectorIndex++) { if (synsAux[0]==wordInfoVector[wordInfoVectorIndex].id()){ // this is the line that's generating a Runtime Error, Why? for (synsAuxCopyIndex=1; synsAuxCopyIndex=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 <wordInfoVector.size(); wordInfoVectorIndex++) { if (antsAux[0]==wordInfoVector[wordInfoVectorIndex].id()){ // this is the line that's generating a Runtime Error, Why? for (antsAuxCopyIndex=1; antsAuxCopyIndex=2 } //--dictionary output function void printWords (ostream &out) { out<<myId<< " "<<word; } //--equals operator for String bool operator == (const string &aString)const { return word ==aString; } //--less than operator bool operator <(const WordInfo &otherWordInfo) const { return word (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 <<(ostream &out, WordInfo &word) { word.printWords(out); } int main() { string wordFile; cout<<"enter name of dictionary file: "<<endl; getline (cin,wordFile); ifstream inStream (wordFile.data()); if(!inStream.is_open()) { cerr<<"cannot open "<<wordFile<<endl; exit(1); } vector  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<<endl<<"the words on the dictionary are: "<<endl; int h=0; while (h<wordInfoVector.size()){ cout<<wordInfoVector[h]<<endl; h++; } aword.dictionaryMenu(wordInfoVector); system("PAUSE"); return 0; }