【问题标题】:Compare variable with each line from a txt file将变量与 txt 文件中的每一行进行比较
【发布时间】:2014-04-25 00:27:58
【问题描述】:

我必须将 txt 文件的每一行与用户输入变量进行比较。 如果用户输入的单词存在于 txt 文件中,它应该提示用户“该单词存在”。如果没有,则退出程序。

这是文本文件的样子:

hello
hey
wow
your

这是我的代码:

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

int main() {

    ifstream file("words.txt");
    string content;
    string userinput;

    while(file >> content) {
        cout << content << endl; // gets all the lines from the txt file

        while(userinput != "exit") {
            // asks user for input
            cin >> userinput;

            // compares two inputs 
            if (userinput == content)
            {
                cout << "The word exists." << endl;
            } else {
                break;
            }

            if (userinput == "exit") {
                break;
            }
        }
    }
    return 0;
}

它不适合我。我能够返回 txt 文件中的所有单词,但无法将用户输入文本与 txt 文件中的 txt 行进行比较。任何帮助都会很棒。谢谢!

更新代码:

    while(iFile >> content) {
        while(userinput != "exit") {
            // asks user for input
            cin >> userinput;

            // compares two inputs 
            if (content.find(userinput) != std::string::npos)
            {
                cout << "The word exists." << endl;
            } else {
                break;
            }

            if (userinput == "exit") {
                break;
            }
        }
    }

P.S:我对 c++ 很陌生。一个学生

【问题讨论】:

  • 您正在对文件中的每个令牌运行一个循环,在其中您要求用户猜测,直到他放弃每个?不错,但不是你说的那样。
  • 你想要做的:读取文件,将其解析为你保存在 unordered_set 中的令牌。然后询问用户应该匹配哪个词。
  • 如果您需要性能提升,请先尝试将单词列表添加到 std::map 或 std::unordered_map (c++11)。然后运行一个 while 循环,在其中您要求用户输入并检查您的 std::map。必要时终止。

标签: c++ string loops


【解决方案1】:
#include <string>
#include <iostream>
#include <fstream>
#include <unordered_set>
using namespace std;

int main()
{
    fstream file("wy.txt");
    string line;
    unordered_set<string> res;

    while(file>>line)
    {
        res.insert(line);
    }

    do 
    {
        cout<<"Please input the word: "<<endl;
        string str;
        cin>>str;
        if (res.find(str) != res.end())
            cout << "The word exists." << endl;
        else
            break;
    } while (true);
}

这段代码可以正常工作。

【讨论】:

  • 我收到一个错误:main.cpp:4:10: fatal error: 'unordered_set' file not found
  • 您使用的是哪个平台?
  • Mac OS,使用 g++ v: 4.2.1
  • 编译代码时请添加-std=c++11这个选项。
  • 这就是我编译它的方式g++ -std=c++11 main.cpp -o app,仍然得到同样的错误
【解决方案2】:

您正在对文件中的每个行标记运行一个循环,在其中您要求用户猜测,直到他放弃每个?不错,但不是你说的。

你想做什么:

  1. 读取文件,将其解析为您保存在std::unordered_set 中的单词标记。
  2. 然后询问用户应该匹配哪个词。
  3. std::unordered_map 泄露其秘密。

试试看。

【讨论】:

  • 谢谢,但不知道如何将其放入代码中,我对 c++ 很陌生,并且是一名学生。
【解决方案3】:

当你写作时:

// compares two inputs 
if (userinput == content)

您确实在检查用户是否在内容中输入了 准确 文本。您想要检查用户输入是否包含在内容中:

// check if the userinput is found in the text
if(content.find(userinput) != std::string::npos)

您还需要阅读完整的文件。现在,每次向用户询问输入时,您都在读取输入文件。

#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>

inline static bool fileExists(const std::string& name) {
   struct stat buffer;
   return (stat (name.c_str(), &buffer) == 0);
}

/// Read the file content and return as a string
static std::string readFile(const std::string &filename)
{
   std::ifstream t(filename);
   std::stringstream buffer;
   buffer << t.rdbuf();
   return buffer.str();
}


int main() {

    std::string filename("words.txt");
    if(!fileExists(filename)) 
       // print error
       return -1;
    string content = readFile(filename);
    string userinput;
    ... // handle userinput from now

请注意,这是低效的。由于您的文本始终相同并且您正在重复搜索,因此可以对其进行预处理。有多种数据结构可以提供帮助。例如,您可以有一个哈希映射并将每一行作为键填充它。

【讨论】:

  • 谢谢,我试过但得到这个错误:main.cpp:20:36: error: use of undeclared identifier 'npos' if (content.find(userinput) != std::npos) 有什么建议吗?
  • 确实:std::string::npos
  • 谢谢,这修复了错误,但它仍然没有这样做。我应该分开两个 while 循环吗?我用更新的代码更新了我的原始帖子。
  • 是的,您必须将两个步骤分开:将文件加载到内存中,然后进行搜索。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 2022-01-06
  • 2011-01-06
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 2013-03-18
相关资源
最近更新 更多