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."

  • 3
    How is the program supposed to know you have finished with the first list of words? – n. m. could be an AI Mar 08 '19 at 09:44
  • Not the actual problem, but you are missing a `#include ` – Jabberwocky Mar 08 '19 at 09:45
  • Not convinced by the duplicate. It doesn't explain why the program terminates without printing `"Program completed."` – john Mar 08 '19 at 09:47
  • @n.m. I think `cin` converts to `false` once you enter invalid input, no? wait... reading a string should not fail under normal circumstances.... – 463035818_is_not_an_ai Mar 08 '19 at 09:50
  • The first thing to say is that the program as written cannot possibly work because it doesn't handle how the two lists of input strings are distinguished. The second thing to say is that despite this it's not clear why the program crashes. It would be understandable for the program to produce no output words, but it's not understandable to me that it doesn't output `"Program completed."` – john Mar 08 '19 at 09:59
  • 1
    When you used a debugger, which line did it report it crashed on? – UKMonkey Mar 08 '19 at 10:04
  • 2
    @john [it probably doesn't](https://ideone.com/MWWsw6). – n. m. could be an AI Mar 08 '19 at 10:11
  • 1
    @n.m. Well yes, but I'm hoping for clarification from the OP. – john Mar 08 '19 at 10:14

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,446
  • 14
  • 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. m. could be an AI Mar 08 '19 at 23:45
  • n.m. well feel free to post an answer then! – Owl Mar 11 '19 at 09:26