Vector exercise Stroustrup book Ch 4.6.

Hello everybody,

Thanks for taking the time to read this.

For self-study purposes, I follow the book Programming, principles and practice using C++, by Bjorn Stroustrup. My question is about the 'Try this' exersize in chapter 4.6. The assignment is as follows:

"Write a program that “bleeps” out words that you don’t like; that is, you read in words using cin and print them again on cout. If a word is among a few you have defined, you write out BLEEP instead of that word. Start with one “disliked word” such as
string disliked = “Broccoli”;
When that works, add a few more."


What I think should happen is the user input being stored in the vector. The compared to the elements of another vector. Based on the the outcome, an if-statement gets activated or not. ( if match = 1 then.. else if match = 0 then...)

I've been struggling with this for over a week. Overcoming range-errors. And Now I suspect something with my loop is wrong. Are there any hints you guys could provide me to steer me in the right direction? I'm not afraid to google or research myself, but I haven't been successful so far.

Regards,

Kim

Here is the code I have created so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include "std_lib_facilities.h"

int i;
int match = 0;


int main()
{
	vector <string> bad_words(4);
	bad_words[0] = "olives";
	bad_words[1] = "tomatoes";
	bad_words[2] = "pickles";
	bad_words[3] = "saus";
	vector <string> user_input(10);


	string input;
	cout << "enter favourite food: ";
	cin >> input;
	user_input.push_back(input);

	for (i = 0; i <= 3; i++)
	{
		cout << user_input[i];
	}


	for (i = 0; i <= 3; i++)
	{
		if (user_input[0].compare(bad_words[i]) == 0)
		{
			match = 1;
		}

	}

	
		if (match == 1)
		{
			cout << "That food is disgusting!" << endl;
		}
		else 
		{
			cout << "what you said was: " << i << endl;
		}
	

	system("pause");
	return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <string>

int main()
{
    // http://www.stroustrup.com/C++11FAQ.html#init-list
    const std::vector<std::string> bad_words { "olives", "tomatoes", "pickles", "saus" } ;

    // read in words using cin
    std::string word ;
    while( std::cout << "word? " && std::cin >> word ) // for each word read from stdin
    {
        // If a word is among a few you have defined, you write out BLEEP
        // we assume that as yet, we have not encountered standard algorithms like std::find
        bool bad = false ;
        // http://www.stroustrup.com/C++11FAQ.html#for
        for( const std::string& bw : bad_words ) // for each word in the vector bad_words
            if( word == bw ) bad = true ;

        if(bad) std::cout << "BLEEP\n" ; // bad word; write out bleep
        else std::cout << word << '\n' ; // otherwise print the word on cout
    }
}
Hello JLBorges,

Thanks for the reply! I see that I might have been thinking a bit too complicated with the incrementations. This seems like an adequate solution. Bool has not been discussed in the book yet, though I do understand it.

I would appreciate some extra clarification for the following line:

for( const std::string& bw : bad_words )
if( word == bw ) bad = true ;

I have read the links you send with it, but I cannot understand why a "&" is placed after std::string. Where does 'bw' suddenly come from? Would you be willing to elaborate a bit more on how this part of the code functions?

Much appreciated!

Kim
> but I cannot understand why a "&" is placed after std::string

You may not have encountered references as yet. For now, write the loop (without the &) as:
for( const std::string bw : bad_words )


> Where does 'bw' suddenly come from?

Read this range-based loop for( const std::string bw : bad_words ) as
'for each string bw in the vector bad_words'

That is, with const std::vector<std::string> bad_words { "olives", "tomatoes", "pickles", "saus" } ;
first time through the loop, bw is equal to "olives",
the second time through the loop, bw is equal to "tomatoes" etc.
up to the last string in the vector.

To see what is going on with that loop, run this small program:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>
#include <string>

int main()
{
    const std::vector<std::string> bad_words { "olives", "tomatoes", "pickles", "saus" } ;

    for( const std::string bw : bad_words ) // for each word bw in the vector bad_words
            std::cout << bw << '\n' ; // print out the word
}
closed account (E0p9LyTq)
I cannot understand why a "&" is placed after std::string. Where does 'bw' suddenly come from?

That loop is a "range-based for loop" (others call it a "for-each loop"):
http://www.learncpp.com/cpp-tutorial/6-12a-for-each-loops/
Topic archived. No new replies allowed.