I reviewed the code -- here's my notes.
I bolded the part that's causing the access violation .. but I recommend you read all of it:
Your checkWord function has a few problems:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
for(int i=0;i<maxUniqueWords;i++)
{
string dbWord = wordDb[i].getName();
if(singleWord.compare(dbWord)==0) // if this word matches the word in the DB
{
// this code runs if the word is NOT unique (Because it matched a word in the DB)
wordDb[i].setCount(1); // therefore this makes no sense
isUnique=1; // and this is backwards
break;
}
else
{
bool isUnique=0; // get rid of this 'bool'. You're creating a second isUnique var and not
// changing the var you mean to.
}
}
|
Although personally I would get rid of the else condition entirely. Initialize isUnique to true and then search for a matching word and set it to false if you find one. If you get through the loop without finding any matches, isUnique is left to it's initialized value of 'true' and you know the word is unique.
|
if(isUnique==0) // Create a new word entry if unique.
|
If isUnique is false... doesn't that mean the word isn't unique?
Or is your variable just named backwards? ... actually now that I see that, it looks like what you're doing is ALMOST right -- except isUnique is backwards and you're never setting it to false.
more from checkWord:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
if(isUnique==0)
{
for(int i=0;i<maxUniqueWords;i++) // why do you need to look for the next available word?
{ // doesn't 'distinctWords' already keep track of that? You can just use it to know where
// to put this word
if(wordDb[i].getCount()==0)
{
wordDb[i].setName(singleWord);
wordDb[i].setCount(1);
break;
}
// else // these lines are all redundant, I'd get rid of them
// {
// continue;
// }
}
}
|
- Word::setName seems like an inappropriate place to increment distinctWords. I would move that to checkWord.
... and actually that is your problem, because setName is called a bunch when you swapValues. This corrupts distinctWords and makes the loop in sort() and indexOfBiggest() run too long.
- Word::setCount is strange:
1 2 3 4 5 6
|
void Word::setCount(int count)
{
wordCount += count; // this is not setting the count, it's adding to it.
// either this function doesn't do what you expect, or it's named
// inappropriately
}
|
- In fix string... this isn't a bug, but it seems wasteful:
1 2
|
string aChar = s.substr(i,1);
int location = punct.find(aChar,0);
|
There's no need to make a separate substr, here. find() works with an individual character. You can just take the character out of the string with the [] operator:
|
int location = punct.find( s[i] ); // much better
|
likewise, line 157 can be changed to
noPunct += s[i];
EDIT:
I also want to say "good work". You're doing a very decent job of organizing your code and splitting it up into manageable tasks. And while it's not perfect, and there are things I would want to change, it's certainly
tons better than most of the stuff we get in here -- which is why I was willing to go over it with a fine tooth comb.
Keep up the good work.
EDIT2:
I also thought this was ironic:
1 2
|
// Avoid name conflicts.
using namespace std;
|
'using namespace std;'
causes name conflicts... it doesn't avoid them. XD