I'm starting to use strings in place of character arrays and am encountering an error when I change one of my character arrays defined with a size of 5 to a string. The error I get is "Expression: string subscript out of range" upon trying to run the program.
"newWord" was originally a character array, but upon changing it to a string I'm getting this error. I don't understand what could be causing this, when using the character array the program performs fine.
int main() {
fstream inputFile;
fstream outputFile;
string newWord;
int i, k;
string word;
inputFile.open( "H:\\word.txt" );
outputFile.open( "H:\\newword.txt" );
if( inputFile )
{
while( getline( inputFile, word ) )
{
for( i = 0; i < (word.length()- 3); ++i )
{
for( k = 0; k < 4; ++k )
newWord[k] = word[i+k];
cout << newWord << endl;
outputFile << newWord << endl;
}
}
}
return 0;
}