Public Group

# Vector Issues - What am i doing wrong?

This topic is 4372 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there everyone, I have been doing an exercise from a textbook and have come a cropper and no matter what i have tried i can't get it to work. Anyway what i am trying to do is read a load of input into a vector, and then find out how many distinct words are in there by comparing the contents of each. Anyhow at the moment when i try and output the contents of my vector it seems to be missing the first and last elements i typed. Anyone know why. Heres my code below.
[source lang  = "cpp"]

//write a program to count how many times each distinct word appears in its input

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

int main()
{
cout<<"Please Enter the Text You Wish to be analysed one word at a time :"<<endl;
string text;
cin>>text;

//put the text in a vector
vector<string> words;
string x;

while (cin>>x)
{
words.push_back(x);
}

typedef vector<string>::size_type vec_sz;
vec_sz size = words.size();

//check that a word was entered if not wuit the program
if (size==0)
{
cout<<endl<<"You Must Enter atleast one word."<<endl;
return 1;
}

//	sort (words.begin(), words.end());
int counter;
counter = 0;

// create a loop to go through each item in the vector
for (vec_sz i = 0; i<(size-1); ++i)
{
//compare the current index with the next - if they are not the same add to counter
if (words != words[i+1])
{
++counter;
}
cout<<words<<endl;
}

//test the last item against all others to make sure its unique
//	if (words[size-1] != words[size-2])
//	{
//		++counter;
//		cout<<words[size-1]<<endl;
//	}

//	cout<<counter<<endl;
cout<<words[0]<<endl;
system("PAUSE");
return 0;
}


for example if i entered: fat bob is very fat the output i get is bob is very Could anyone shed light on what i am doing wrong and point me in the direction of a solution that would be great. Cheers all. I shall continue to wrack my brain and see if i beat any of you smart lot to a solution. Ta Neil

##### Share on other sites
not sure why it's missing the first, but here's why it's missing the last:

for (vec_sz i = 0; i<(size-1); ++i)

assume the size is 5 (i.e. 5 words in the vector).

for( i = 0; i < (5-1); ++i )

you will print the following:

0
1
2
3
-> you won't print 4 because 4 is not < 5-1

the for loop should look like:
for (vec_sz i = 0; i<size; ++i)

actually, though, for iterating vectors it's cleaner to do the following because it will allow you to easily switch over to list or whatever later on if you need to:

vector<string>::iterator ite = words.end();for ( vector<string>::iterator it = words.begin(); it != ite; ++it ){    // *it is the item in the list}

[WARNING: my fix will break the if statement inside your loop. you need to fix that so it doesn't compare against the next word if it's the last word in the list]

-me

p.s. wth does "come a cropper" mean? =)

##### Share on other sites
Thanks for that,

I shall try and fix the inner loop now and see what happens.

Come a cropper probably wasn't the right phrase but it basically means that i've tried to do something that i couldn't and been caught out.

##### Share on other sites

You have cin>>text

then the loop goes

cin>>x

before text is stored.

##### Share on other sites
Bounding issues are easy to make, but when using iterators frequently can avoid them.

If you want to still use indexes you can use:

[source lang = "cpp"]	int counter;	counter = 0;	// create a loop to go through each item in the vector	for (vec_sz i = 0; i < size; ++i)	{		//compare the current index with the next - if they are not the same add to counter		if (i+1 < size && words != words[i+1])		{			++counter;		}		cout<<words<<endl;	}

##### Share on other sites
Thanks for that, I'm at chapter 3 in this book as you could probabaly tell from the schoolboy nature of my error.

Still can't figure out what i am doing wrong with regards to the first element dissapearing but what can you do?

##### Share on other sites
I missed your first post about the cin bit, but having reviewed it and had a look i have managed to remedy my error.

Thanks to you two my program actaully does what its supposed to do.

Cheers

The new code is here

[source lang = "cpp"]//write a program to count how many times each distinct word appears in its input#include <iostream>#include <algorithm>#include <iomanip>#include <string>#include <vector>using namespace std;int main(){	cout<<"Please Enter the Text You Wish to be analysed one word at a time :"<<endl;	vector<string> words;	string text;		while (cin>>text)	{		words.push_back(text);	}	typedef vector<string>::size_type vec_sz;	vec_sz size = words.size();		//check that a word was entered if not wuit the program	if (size==0)	{		cout<<endl<<"You Must Enter atleast one word."<<endl;		return 1;	}	sort (words.begin(), words.end());	int counter;	counter = 0;	// create a loop to go through each item in the vector	for (vec_sz i = 0; i<size; i++)	{		//compare the current index with the next - if they are not the same add to counter		cout<<i<<endl;		//if your not at the last item and the words are different add to the counter		if (i+1 < size && words != words[i+1])		{			++counter;		}		cout<<words<<endl;	}	if (words[size -1] != words[size-2])	{		++counter;	}	cout<<"There are "<<counter<<" different words in that text"<<endl;	system("PAUSE");	return 0;}

##### Share on other sites
Out of uttery curiosity is this an exercise from the book C++ Primer by Stanley Lippman?

##### Share on other sites
Quote:
 Original post by neilski_2003Thanks for that, I'm at chapter 3 in this book as you could probabaly tell from the schoolboy nature of my error.Still can't figure out what i am doing wrong with regards to the first element dissapearing but what can you do?

As an above poster pointed out, you're throwing away the first entry:

cout<<"Please Enter the Text You Wish to be analysed one word at a time :"<<endl;string text;cin>>text;

the first word is stored in the text variable, but then you call cin>>x which gets the next word and you store the next one. the word stored in text is never put into your vector. that variable just plain ol shouldn't exist.

Incedentally, this is where using a debugger would come in handy. by stepping through the program you could see exactly what's getting stored where and why.

-me

##### Share on other sites
Saruman,

Nope its from "Accelerated C++" but i'd imagine a liot of the books have similar exercises as they should all cover much the same stuff.

To be precise its exercise 3-3.

Cheers
Neil

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 25
• 9
• 57
• ### Forum Statistics

• Total Topics
632640
• Total Posts
3007618
• ### Who's Online (See full list)

There are no registered users currently online

×