Link to home
Start Free TrialLog in
Avatar of businessesatoz
businessesatoz

asked on

initialize multiple words in string c++

Hello, I wrote a simple program that bleeps out words i don't want. Although i don't know how to initialize multiple strings into my disliked string.

right now i have "string disliked="Broccoli" but i want to also insert few more words but i don't know how.

#include "learning.h"

int main()

{

	//collect the words first (try one word first)
	vector<string>words;

      string word;
	  string disliked="Broccoli";
	 
	  

	while (cin >> word)

		words.push_back(word);

	for(int i = 0; i<words.size(); ++i)

		if(words[i]!=disliked)
			cout <<words[i] <<"\n";

		else
			cout  <<"bleep, Word number:  " <<i;


	keep_window_open();
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

Just like you fill the words vector, you can also fill the disliked vector.

For every word that you read, you can check whether it's in the disliked vector - using the find algorithm eg. :

        http://www.cplusplus.com/reference/algorithm/find/
Avatar of businessesatoz
businessesatoz

ASKER

so you mean make a new vector for disliked words is that correct?
I'm sorry.. i have no idea how to do that.. is their another way around it?
>> i have no idea how to do that..

Exactly the same way you already did it for the words vector.

Which part are you having difficulty with ?
I know how to create the vector, but i can only test 1 value at a time..how do insert more the 1 value in my if statement.. i have upated my code ]including the vector but only allows me to test the single value at a time.. how do i have it test more then the single value.
#include "learning.h"

int main()

{

	//collect the words first (try one word first)
	vector<string>words;
	vector<string>disliked(3);

      string word;
	  string w;
	  disliked.push_back(w);

	  disliked[0]="Broccoli";
	  disliked[1]="Bhavdip";
	  disliked[2]="Beans";
	  
	 

	while (cin >> word)

		words.push_back(word);

	for(int i = 0; i<words.size(); ++i)

		if(words[i]!=disliked[0])
			cout <<words[i] <<"\n";

		else
			cout  <<"bleep, Word number:  " <<i;


	keep_window_open();
}

Open in new window

>> how do i have it test more then the single value.

As I mentioned in my first post - you can use the find algorithm for that :

        http://www.cplusplus.com/reference/algorithm/find/

Have a look at the code example at the bottom of that reference page to see how you can use it for a vector.

Instead of comparing against just one disliked word, you use the find algorithm to check if the word can be found in the disliked array.
yes, i looked at what you provided but i don't understand how i can apply it to my sample program.
if(words[i]!=(find (words.begin(), words.end(),0)))

Open in new window


is that right? i don't think that is right :(
You want to look if the word exists in the disliked vector.

The first two arguments refer to the beginning and end of the vector you want to look into - so in this case, that would be the disliked vector (not the words vector).

The third argument refers to the word you want to look for, so that would be words[ i ].

find will return an iterator to the word if it's found, and will disliked.end() if it's not found. So, you can compare the return value against disliked.end() to see if it was found or not.
but i don't think i have my syntax correct... can you provide me the code so i can learn from it? thank you .
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks.. i feel so discourge sometimes i spend hours trying to figure it out. However i will study this . you will see many post from me lol as i'm learning
thank you.
We've all been new to C++ at some point, so we've all been there.

Any time you need help, just ask here, and someone will assist you.
also just curious but happens here when you compared everything to
!=disliked.end

Open in new window

As I explained earlier, the find function returns either

(a) an iterator that points to the word in the disliked vector (if it was found there)
(b) the end iterator for the disliked vector (if the word was not found in it)

Please refer to the reference page for the find function for more details (link posted earlier).

So, you can check the return value of the find function call to see if it's equal to disliked.end() or not in order to know if the word was found or not. And that's exactly what the code I posted is doing.
yeah i got it going finally. :) thanks i understand.