视觉程序在C++中使用VECTORS删除不喜欢的单词?[这是PPP第4章中的"Try This"问题]

visual Program to remove disliked words using VECTORS in C++? [This is a "Try This" quesn in PPP Chapter 4]

本文关键字:PPP 这是 4章 Try 问题 This 单词 C++ 程序 VECTORS 视觉      更新时间:2023-10-16
#include "..//..//std_lib_facilities.h"
int main()
{
     vector<string>disliked ;
     cout<<" Enter the words you don't like :- ";
     string dis;
     while(cin>>dis)
         {disliked.push_back(dis);
          cout<<"To terminate the input press ctrl+Z";
         }
     cout<<"Enter a sentence :-";
     vector<string>sentence;
     string word;
     while(cin>>word)
         {
          sentence.push_back(word);
          cout<<"To terminate the input press ctrl+Z";
         }
     for(unsigned int i=0;i<sentence.size();i++)
          {for(unsigned int n=0;n<disliked.size();n++)
              {if (sentence[i] == disliked[n])
               sentence[i] = "Bleep";
              }
          cout<<sentence[i];
          }
}

当我运行程序时,执行在第一次输入后停止。它不允许我在句子向量中输入单词

一旦输入流接收到文件结束条件,所有进一步从输入流读取的尝试,而不仅仅是第一次尝试,将以文件结束条件失败。

因此,当您使用CTRL-Z在标准输入上发送文件结束时,当您试图从输入流中读取更多输入时,您继续在输入流上接收文件结束条件。

尽管在交互式终端中可能有也可能没有清除文件结束条件的方法,但最简单的方法是找到其他方法来表示第一组输入的结束,而不是按CTRL-Z表示文件结束。