函数无法识别变量值?

时间:2020-09-11 00:14:59

标签: c++

好吧,标题有点不确定,但是无论如何我都无法弄清楚标题的标题。基本上,正在发生的事情是,我需要通过检查“难题”中是否存在单词来确定“难题”中的一行是否是单词。 (在大多数情况下)如果我在while循环中为if语句传递了字符串文字(带有注释),则该方法有效(但在我传递'word'变量时则无效)。有什么想法吗?我不知道。 LMK如果您需要更多信息。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


void checker(char* puzzle, char* dict){
    fstream newfile;
    string line;
    string word;

     newfile.open(puzzle, ios::in);
    if (newfile.is_open()){ 
        getline(newfile, word);
        newfile.close();
    }

    newfile.open(dict, ios::in); 
    if (newfile.is_open()){ 
        bool found = false;
        while (!found && !newfile.eof()){
            getline(newfile, line);
            if (line.find(word) != string::npos){ // this line. If i replace word with "can" for example, it will work as "can" is in the dict file.
                found = true;
            }
        }
        newfile.close();
        if (!found) {
            cout << "Not found" << endl;
        } else {
            cout << word << " is a word." << endl;
        }
    }
}

0 个答案:

没有答案