C++ For-in-while循环只运行一次(c+;+;)

C++ For-in-while循环只运行一次(c+;+;),c++,C++,我试图在字典中搜索,并检查向量中的一些单词是否在字典中 我目前正在使用while循环进行此操作,其中包含for循环: #include <iostream> #include <fstream> #include <string> #include <vector> #include <string> using namespace std; void wordCheck () { ifstream inDict("linux

我试图在字典中搜索,并检查向量中的一些单词是否在字典中

我目前正在使用while循环进行此操作,其中包含for循环:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>
using namespace std;

void wordCheck () {
    ifstream inDict("linux_dict_words.txt");
    if(!inDict) cerr << "Couldn't open ";
        vector<string> across;
    across = {"tide", "slot", "add"};
    vector<string> tmpWords;
    string tmpLine;
    while(getline(inDict, tmpLine)) {
        for (int i = 0; i < tmpLine.length(); i++) {
            tmpLine[i] = tolower(tmpLine[i]);
        }
        tmpWords = across;
        across.clear();
        for (int i = 0; i < tmpWords.size(); i++) {
            if (tmpLine == tmpWords[i]) {
                across.push_back(tmpWords[i]);
            }
            cout << tmpLine << '\n';
        }
    }
}

int main () {
    wordCheck();
    return 0;
}
预期产出:

a
a
a
as
as
as
add
add
add
等(字典文件中的每个单词打印3次)


这将输出字典中的第一个单词“a”3次。但是,它应该将字典中的每个单词输出3次。我可以确认它正在解析整个字典,因为for循环之外的cout语句会输出字典中的所有单词。

欢迎使用堆栈溢出!听起来您可能需要学习如何使用调试器逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:
tPLINE
for
循环中不会更改。这就是为什么您会得到几个相同的输出。什么是
指示
?另外,你应该为这类问题贴一个帖子。你的外循环,唯一改变
tmpLine
的东西,在你的内循环完成迭代之前,是不会迭代的。因此,您的内部循环只是重复输出相同的变量;发布一个我们可以实际运行的日志。
a
a
a
as
as
as
add
add
add