#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;
}
编写一个程序来消除您不喜欢的单词。首先输入您不喜欢的单词列表。打印"输入单词列表"后将终止程序。
而不是cin.sync()使用cin.clear();
您也可能需要使用cin.ignore()。
问题是您在CIN中陷入困境,并且会阻止任何未来的CIN条目。控制D关闭系统管道。该程序立即退出。
如果您检查结束输入列表的输入,可能会更有用。
使用cin.sync()执行:
$ ./a.out
Enter the word that you dislike.
test
a
b
c
^d
Enter the list of words.
Program completed.
$
用添加cin.clear()和cin.ignore()替换cin.sync()后执行。
$ ./a.out
Enter the word that you dislike.
test
a
b
c
^d
Enter the list of words.
a
b
c
^d
Program completed.
$