exercise in c++ book that bleeps out words

Feb 24, 2019 at 8:09pm
Why is there no output after I input many words separated by spaces?
Also, how do I add std_lib_facilities.h

1
2
3
4
5
6
7
8
9
10
11
12
13
  int main() {
	vector<string> words;
	vector<string> disliked = { "one", "two", "three", "four" };
	for (string word; cin >> word;)
		words.push_back(word);
        sort(words);
	for (int i = 0; i < words.size(); ++i)
           if (word[i]= disliked)
                cout<<"BLEEP"<<'\n';
           else if(i==0||words[i-1]!=words[i])
		cout << words[i] << '\n';

}
Feb 24, 2019 at 8:23pm
Your code doesn't compile (it is lacking headers and your "sort" function).

What do you mean by "how do I add std_lib_facilities.h"?
If I recall, it's a header from a Stroustrup book, available online?
Just download it and include it.
Looks like it's here: http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
(and it has "sort" in it).
Last edited on Feb 24, 2019 at 8:27pm
Feb 24, 2019 at 8:26pm
It does have headers I just did not copy paste them over.
Feb 24, 2019 at 8:30pm
It does have headers I just did not copy paste them over.

Well that doesn't help me, does it. :-) Post complete code (you can edit your post).

Why are you sorting the words? That doesn't make sense to me.

For this: word[i] = disliked , you probably meant to use == . A single = is assignment, not comparison.
But even that isn't correct.
You need to loop through the "disliked" words and compare them to word[i] one at a time.

Also, you will need to signal the "end-of-file" condition after entering the words.
On windows you do that by typing "Ctrl-Z" on a new line. On linux (and macOS?) you use "Ctrl-D".
Last edited on Feb 24, 2019 at 8:36pm
Feb 24, 2019 at 8:47pm
Sorry, here's what i have now. it still doesnt work.

#include<iostream>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include<vector>
#include<algorithm>
#include<stdexcept>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }

int main() {
vector<string> words;
vector<string> disliked = { "one", "two", "three", "four" };
for (string word; cin >> word;)
words.push_back(word);
for (int i = 0; i < words.size(); ++i)
for (int j = 0; j < disliked.size(); ++j)
if (words[i] == disliked[j])
cout << "BLEEP" << '\n';
else
cout << words[i] << '\n';
keep_window_open();

}
Last edited on Feb 24, 2019 at 8:47pm
Feb 24, 2019 at 11:44pm
Hello houndbob,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



A few blank lines would make your code easier to read.

What starts in "main" with the for loop for entering is a problem. There is no way out of the loop. I had to enter ^z to exit, but this left "std::cin" in a failed state.

So when you get to the function "keep_window_open():" "std::cin" is in a failed state and nothing happens in the function. It just returns. As an alternative to what you have you may find this of more use:
1
2
3
4
5
inline void keep_window_open()
{
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();
}

Not sure if "inline" is worth keeping, but it does work. The "std::cin.get()" will allow just the "Enter" key to be pressed. Whereas yours you have to enter something and then press "Enter".

In "main"
1
2
for (string word; cin >> word;)
    words.push_back(word);

This works, but look at it closely. How do you end this loop? I found that this work better:
1
2
3
4
5
6
std::cout << "\n Enter some words. type (end) to quit.\n" << std::endl;

for (string word; cin >> word && word != "end";)
	words.push_back(word);

std::cout << std::endl;

The last line is to break up the output to the screen.

Also this leaves "std::cin" in a good state that can be used later.

The nested for loops need some work.

As dutch said:
You need to loop through the "disliked" words and compare them to word[i] one at a time.

The "else" statement is printing out to many words. You do not need the "else" part and the "std::cout" should be at the end of the outer for loop. This is only part of what is needed. You still need a way to print the "BLEEP" and not the word it replaces.

I have made the program work. Now it is your turn to try to make the necessary changes.

Hope that helps,

Andy
Feb 25, 2019 at 12:41am
Thank you so much Andy!
Topic archived. No new replies allowed.