0
#include<iostream>
#include<vector>
#include<ios>
#include<limits>
int main()
{
     using namespace std;
     vector<string> disliked,words;
     int n;
     cout<<"Enter the word that you dislike."<<endl;
     for(string word;cin>>word;)
         disliked.push_back(word);
     cout<<"Enter the list of words."<<endl;
     cin.sync();
     for(string word;cin>>word;)
         words.push_back(word);
     for(int i=0;i<words.size();i++)
     {
         int n=0;
         for(int j=0;j<disliked.size();j++)
         {
             if(disliked[j]==words[i])
                 n++;
         }
         if(n==0)
         cout<<words[i]<<endl;
     }
     cout<<"Program completed."<<endl;
     return 0;
}

Write a program to bleep out the word that you don't like.First input the list of words that you don't like. Program terminates after printing "Enter list of words."

1 Answers1

1

Instead of cin.sync() use cin.clear();

You may also need to use cin.ignore() also.

The problem is you have a ^D stuck in cin and it's blocking any future cin entries. Control D closes the system pipe. And the program immediately exits.

It might be more usable if you check for an input that ends the input list.

Execution using cin.sync():

$ ./a.out 
Enter the word that you dislike.
test
a
b
c
^d
Enter the list of words.
Program completed.
$ 

Execution after replacing cin.sync() with adding cin.clear() and cin.ignore():

$ ./a.out 
Enter the word that you dislike.
test
a
b
c
^d
Enter the list of words.
a
b
c
^d
Program completed.
$
Owl
  • 1,325
  • 12
  • 20
  • You're probably right, but this isn't consistent with what the OP is reporting. – john Mar 08 '19 at 10:15
  • It's 100% what OP is asking. When you run the code, it exits after Enter list of words. Exactly what OP asked. – Owl Mar 08 '19 at 10:18
  • @Owl the solution you suggest is correct (upvoted), but on my system the program does not terminate before the line of code you mention in your comment. – Patrick Trentin Mar 08 '19 at 10:20
  • Sorry i meant after. Which is what OP asked. (typo) – Owl Mar 08 '19 at 10:21
  • @Owl Ctrl-D is the end of file indicator, I would not expect that to terminate a program. – john Mar 08 '19 at 10:22
  • ??? The program correctly terminates at `return 0;`. The only issue is that there's nothing inside `words` because of `^D`, as you correctly pointed out. – Patrick Trentin Mar 08 '19 at 10:22
  • 1
    @Owl This is the issue, the OP is reporting a crash. He may well be wrong (he probably is) but that's what I meant when I said your answer is not consistent with what the OP is saying. – john Mar 08 '19 at 10:23
  • John / Patrick: You're correct it should print out the last cout though. It's not clear if the program crashes or not, it seems unclear from the OP, I think we need more info. – Owl Mar 08 '19 at 10:54
  • Thanks Owl. Where did you learned this things?Any book should I read or any website I have to learn from. Please reply. – Mrugesh Raulji Mar 08 '19 at 15:35
  • Thanks Owl.But there is another problem that is it does not take the first character of first input for second cin. How can I fix that? – Mrugesh Raulji Mar 08 '19 at 15:45
  • Remove the cin.ignore() command, and only use the cin.clear(). The ignore drops the first character. – Owl Mar 08 '19 at 17:10
  • cin.clear() will not work for input that is not a terminal, and there's no guarantee it will work for the terminal either. – n. 1.8e9-where's-my-share m. Mar 08 '19 at 23:45
  • n.m. well feel free to post an answer then! – Owl Mar 11 '19 at 09:26