I'm sorry about the question but I have been studying this for a while and can't figure out where the error is. This is a translator function that should be able to get words from getline and then match them with another array of words the index of which returns the translated word. The function crashes before it reaches the end however.
void Translator::toElvish(char out_s[], constchar s[])
{
int wordCount = 0;
int charCount = 0;
char engW[50];
char fullWord[50];
dictionary(Dictionary);
for (int i = 0; i < strlen(s); i++) //strlen of getline
{
char c = s[i]; //c = the position of the getline
if (c !=' '||'\t'||'\n'||','||'.') //if c is a character
{
engW[charCount] = c; //we add another character
charCount++; //We count the character added
}
cout <<engW;
if (c == ' '||'\t'||'\n'||','||'.') //if c = space
{
for (int j = 0; j < charCount; j++) //we loop through the character count inengW
{ //we increment word count and make full word = engW
wordCount++;
fullWord[j] = engW[i-charCount];
}
}
charCount = 0;
}
for (int i=0; i<numEntries; i++) //Looking up the words
{
if (strcmp(englishWord[i], fullWord)==0)
{
break;
}
if (i < numEntries)
{
strcpy(out_s, elvishWord[i]);
}
}
}
no thace. numEntries will always be what the set value is ( 805) and anways if it has 805 values the array's last position is really 804 so you should use i instead since i is iterating from 0 - 804
You are certain that testing the same element repeatedly is the way to search an entire array? in any case that element will be out of bounds, causing the crash.
No, I'm certain that is correct. There are 805 numEntries and I need to search through all of them to find the correct English word.
So, if we assume that numEntries is 805 as you say it is, which of the entries does englishWord[numEntries] refer to?
1 2 3 4 5 6 7 8 9
int main()
{
constunsigned numEntries = 2;
int array[2] = { 0, 2 } ;
// valid indices for array, which has 2 elements are 0 and 1.
// so if I do array[numEntries] which is the element that I'm accessing?
// neither - numEntries is an invalid index and out of bounds of our array.
}
You may also note that, even were numEntries a valid index, you would be comparing the same element of englishWord to the same word each iteration of the loop.
Your not making much sense to me. My lecturer pretty much supplied that piece of code for use which is why i'm confident it can be applied. Why would numentries not be a valid index? numEntries = 2 would return the third result. I don't see why it would not.
arrays start at index 0 and if you use the same index each time the value will not change in cire's example in array positon 0 or array[0] is the number 0 and positon 1 is the number 2 there is no positon 2 that would mean you have an array size of 3
look at this link http://www.cplusplus.com/doc/tutorial/arrays/
What everyone is trying to point out to you is that numEntries is accessing some value out-of-bounds of the array you want to check.
From cire's example:
1 2
constunsigned numEntries = 2;
int array[2] = {0,2};
And then what you just stated:
numEntries = 2 would return the third result. I don't see why it would not.
array in cire's example only has two items. What are you accessing if you attempt to get the third item?
In the same way, you current code is accessing a value just beyond the array's bounds and you do it a numEntries times.
1 2 3 4 5 6 7 8 9 10 11 12 13
for(int I = 0; I < 805; I++){ //Let's go ahead and substitute your number to see what happens
//Note that you don't use the variable I anywhere in this block
if (strcmp(englishWord[805], fullWord)==0)
{ //So now you access some value at position 805 when englishWord only goes up to 804;
//englishWord at position 0 counts as the first position
cout << fullWord;
out_s = elvishWord[805]; //Let's go out of bounds again
}
/*Another thing to note that everyone else has tried telling you
is that you are accessing the same position for each iteration. You
are not iterating through each word.
*/
}
Why would numentries not be a valid index? numEntries = 2 would return the third result. I don't see why it would not.
You are correct. array[2] would access the nonexistent 3rd element of an array that only has 2 elements. I hope you can see what's wrong with that and why your confidence is misplaced.