It goes "BLEEP" when one of these strings are input.

int main()
{
	vector<string> words();
	words[0] == "roller coasters";
	words[1] == "waiting";
	words[2] == "people who talk on the phone while driving";
	words[3] == "wars";

	string temp;
	while (cin >> temp)
		words.push_back(temp);

	for (int i = 0; i < words.size(); ++i)
		if (words[i -1] == words[i])
			cout << "BLEEP" << endl;

	return 0;
}

Recommended Answers

All 13 Replies

vector<string> words();
	words[0] == "roller coasters";
	words[1] == "waiting";
	words[2] == "people who talk on the phone while driving";
	words[3] == "wars";

You haven't allocated any memory for the vector.

Try this :

vector<string> words(4); //Create 4 elements for now
	words[0] == "roller coasters";
	words[1] == "waiting";
	words[2] == "people who talk on the phone while driving";
	words[3] == "wars";

That's still wrong -- use the = assignment operator, not the == boolean operator.

vector<string> words(4); //Create 4 elements for now
	words[0] = "roller coasters";
	words[1] = "waiting";
	words[2] = "people who talk on the phone while driving";
	words[3] = "wars";

Or use push_back() method if you don't know how many strings will be added to the vector

vector<string> words; //Create 4 elements for now
	words.push_back("roller coasters");
	words.push_back("waiting");
	words.push_back("people who talk on the phone while driving");
	words.push_back("wars");

use the = assignment operator, not the == boolean operator.

Its getting late,here.

yes, push_back() works. since i don't know how many strings i am going to add. thanks, guys!

This is a big problem:

for (int i = 0; i < words.size(); ++i)
     if (words[i -1] == words[i])

The first iteration of the loop 'i' will be 0. So when you replace 'i' with 0, you get this: if (words[0 -1] == words[0]) You're trying to access the -1th element of the vector, which does not exist!

This is a big problem:

for (int i = 0; i < words.size(); ++i)
     if (words[i -1] == words[i])

The first iteration of the loop 'i' will be 0. So when you replace 'i' with 0, you get this: if (words[0 -1] == words[0]) You're trying to access the -1th element of the vector, which does not exist!

yes, sir. that's what i thought when i got an error. i am trying to figure out. a little help?

int main()
{	
	vector<string> words;
	words[0] = "roller coasters";
	words[1] = "waiting";
	words[2] = "people who talk on the phone while driving";
	words[3] = "wars";
	string temp;
	while (cin >> temp)
		words.push_back(temp);

	sort(words.begin(), words.end());

// supposed to bleep if the input matches with words above
	for (unsigned int i = 1; i < words.size(); ++i)
		if(i != 0 || (words[i - 1] == words[i])) //check previous word if they match once sorted?
			cout << "bleep" << endl; //if so, bleep!


	
	return 0;
}

once again, thank you, all!

line 28: why is it testing for i != 0??? I is initialized to 1, so it will never be 0.

line 28: why is it testing for i != 0??? I is initialized to 1, so it will never be 0.

i was assuming that if i == 0, then words would make it i == -1, and that violated vector rules?

so that doesn't even need to be there? i keep getting runtime error.
if (i<0||this->size()<=i) throw Range_error(i);


for (unsigned int i = 0; i < words.size(); ++i)
//if i == 1 by now then why does it seem like  i < 0 after the line below? ;[
		if(words[i - 1] != words[i])
				cout << "bleep" << endl;

>>i was assuming that if i == 0, then words would make it i == -1, and that violated vector rules?

Impossible. If you initialize i = 1 then it can never ever be 0 without creating a data overflow condition by incrementing i too many times.

>>for (unsigned int i = 0; i < words.size(); ++i)
That is NOT the same as what you posted before, which is what I responded to. IMO you are making the wrong comparisons

for(size_t i = 0; i < words.size()-1; ++i)
{
    if( words[i] == words[i+1] )
    {
        // blabla
    }
}

i am stuck! can someone re-write that FOR and IF part then explain to me? thanks much! every comments that you have left always teach me something.

btw, i am supposed to keep the code as simple as i can, right?

vector<string> words;
	words[0] = "roller coasters";
	words[1] = "waiting";
	words[2] = "people who talk on the phone while driving";
	words[3] = "wars";
	string temp;
	while (cin >> temp)
		words.push_back(temp);

	sort(words.begin(), words.end());


	for (unsigned int i = 0; i == words.size(); ++i)		
		if(words[i] == words[i+1]) //check next word if they match?		
		 cout << "bleep" << endl; //if so, bleep!

	return 0;

read the loop I created for you more carefully -- what you posted isn't the same.

why are you confusing the "=" with "==" ?

Do you know what the difference is ?

"=" is called the assignment operator. It assigns a value to a variable.
"==" compares two comparable object/data types

In your for loop you have " i == words.size()". This is a bug,
since " i " will go one passed the end and evaluate the loop.

Change this :

for (unsigned int i = 0; i == words.size(); ++i)		
		if(words[i] == words[i+1]) //check next word if they match?		
		 cout << "bleep" << endl; //if so, bleep!

to

for (unsigned int i = 0; i < words.size() - 1; ++i)		
		if(words[i] == words[i+1]) //check next word if they match?		
		 cout << "bleep" << endl; //if so, bleep!

The new things there is i < words.size() - 1; Its words.size() - 1 because
in your loop you are checking " i " to " i + 1". Thus if I was at the
last element, it would compare the last element, to one passed the end
which is junk.

Thanks, guys! Now i understand.

I was assuming ... For instance, i pusH 2 more strings, "cigarettes" and "waiting" so we have 6 elements.

For( int i = 0; 0 < 6; 1)
if ( "Waiting" == "roller coasters")
no cout

second round
for ( int i = 1; 1 < 6; 2)
if( "waiting" == "waiting")
cout << "bleep"

and so on.

Have a good night guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.