0

I'm supposed to read an integer n from the user, which is the followed by n words, and then what follows after that is a sequence of words and punctuation terminated by the word END. For example:

2 foo d  
foo is just food without the d . END

The n words are to be "redacted" from the second line. So it would show up as:

*** is just food without the * . 

I think I can figure out the redacting part. I just cannot seem to figure out how to read the words in... any help is much appreciated!

#include <iostream>
#include <string>
using namespace std;

int main()
{
int n;
cin >> n;

string *redact = new string[n]
for(int i = 0; i < n; ++i)
    cin >> redact[i] // this part works!

return 0;
}
3
  • Your code sample doesn't even compile. Commented Nov 4, 2015 at 11:50
  • @πάνταῥεῖ missing ; ... panic? Commented Nov 4, 2015 at 11:51
  • 3
    @AntiHeadshot No panic, just stated the fact. OP should post their real code. Commented Nov 4, 2015 at 11:52

1 Answer 1

2

Following code will cater the purpose.

#include <iostream>
#include <string>
#include <set>

int main()
{
  int n;
  std::cin >> n;

  std::set<std::string> redact;
  std::string word;
  for(int i = 0; i < n; ++i)
  {
    std::cin >> word;
    redact.insert(word);
  }
 while( std::cin>> word && word != "END" )
 { 
     if ( redact.find(word) == redact.end() )
        std::cout<<word<<' ';
 }
 std::cout<<'\n';
return 0;
}

I believe you are a learning C++, please note a point use typesafe, bound-safe and scope-safe C++.

So, no new delete unless you can call yourself an adept. Use the algorithms and containers provided by C++, instead of inventing your own.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.