I am having a little problem. A variable holding an integer is changing during a function call that has nothing to do with the variable. The only thing that I could think of is perhaps the function is overwriting the memory location the variable is in?
Here is the relevant section of main():
cin.getline(usr_input,INPUT_LENGTH); cout<<"\n1 1:"<<words_in_1<<"\n 2:"<<words_in_2<<"\n 3:"<<words_in_3; getWord(usr_input, word, char_marker, INPUT_LENGTH, WORD_LENGTH); cout<<"\n2 1:"<<words_in_1<<"\n 2:"<<words_in_2<<"\n 3:"<<words_in_3;
And here is the function getWord():
void getWord (char array[], char word[], int ch_place, int inputLength, int wordLength)
{
/* * Takes input of the character array[] and places all characters from array[ch_place]
* to the first space encountered/the end of array[] */
int i; // Need i outside for loop
/* From i=0, loop while we have not reached the end of users input, the ith array character is not
* a null or a space */
clean(word,wordLength);
for ( i=0; (array[ch_place] != ' ') && (array[ch_place] != '\0') && (ch_place<inputLength); i++, ch_place++)
{
word[i] = array[ch_place];
}
word[i] = '\0'; // Ensure the array word is terminated with a null character
}
And clean():
int clean(char array[], int array_length)
{
for (int i=0; i <= array_length; i++)
{
array[i]='\0';
}
return 0;
}
And here is the output over the few lines of main():
Quote
1 1:0
2:0
3:3
2 1:0
2:0
3:0
2:0
3:3
2 1:0
2:0
3:0
This does not happen with words_in_1 nor words_in_2, and as far as I can see throughout the rest of my code words_in_* are always used in exactly the same way (just being for a different list #).
Any help, ideas or pointers would be greatly appreciated,
enpey

New Topic/Question
Reply



MultiQuote


|