当我从我的程序中读取一个包含 5 个单词的 .txt 文件并将其放入一个有 20 个空格的数组中时,我文件中的最后一个单词填满了我数组中的最后 16 个位置。任何想法为什么?我输入的文件最多有 20 个字。
newArray string[20];
if (inputFile) {
while (i<20) {
inputFile >> word;
if (word.length()<2) { //gets rid of single character words
i++;
}
else{
newArray[i] = word;
cout<<newArray[i]<< " ";
}
}
inputFile.close();
}