【发布时间】:2013-01-26 06:49:14
【问题描述】:
我终其一生都无法弄清楚为什么这不起作用。我必须对文件中的单词列表进行频率检查,并且在读取它们时,我试图根据字符串数组中的元素检查当前单词,并确保它们在我之前不相等添加它。代码如下:
fin.open(finFile, fstream::in);
if(fin.is_open()) {
int wordArrSize;
while(!fin.eof()) {
char buffer[49]; //Max number chars of any given word in the file
wordArrSize = words.length();
fin >> buffer;
if(wordArrSize == 0) words.push_back(buffer);
for(int i = 0; i < wordArrSize; i++) { //Check the read-in word against the array
if(strcmp(words.at(i), buffer) != 0) { //If not equal, add to array
words.push_back(buffer);
break;
}
}
totNumWords++; //Keeps track of the total number of words in the file
}
fin.close();
这是一个学校项目。我们不允许使用任何容器类,所以我构建了一个结构来处理扩展 char** 数组、推回和弹出元素等。
【问题讨论】:
-
@Alex,他到底为什么不应该问作业问题?
-
@SingerOfTheFall 我以为他们被禁止了?
-
@SingerOfTheFall 够公平的。我不能再编辑我的评论了,所以我将删除,因为其中一个答案澄清了我试图指出的内容。
标签: c++ arrays input char duplicates