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;
}
| |