【问题标题】:How to: C++ Reading a File and using the "if statement"如何:C++ 读取文件并使用“if 语句”
【发布时间】:2020-03-12 10:39:24
【问题描述】:

我正在尝试从文件中读取并检查文件中的单词,使用 if 语句,如果文件中有单词,我希望它输出某个文本。

我不太确定我做错了什么,有人可以启发我吗?请帮助而不是称我为白痴或其他什么。我想学,我是C++和编程菜鸟。

这是到目前为止文件文本中的逐行内容:

女8D

男7E

男8D

女7B

void fourCoats() 
{
    string word;

    ifstream horseList;
    horseList.open("horseList.txt");
    if (!horseList.is_open())
    {
        cout << "Unable to open file...";
        exit(1);
    }



    while (horseList >> word)
    {

        if (word == "Male8D") {
            while (horseList >> word) {
                if (word == "Female7B") {
                    cout << "You have Male8D and Female7B.\n";
                    break;
                }
            }
        }
        else if (word == "Female8D") {
            while (horseList >> word) {
                if (word == "Male7B") {
                    cout << "You have Female8D and Male7B.\n";
                    break;
                }
            }
        }
        else if (word == "Male8D") {
            while (horseList >> word) {
                if (word == "Female7C") {
                    cout << "You have Male8D and Female7C.\n";
                    break;
                }
            }
        }
        else if (word == "Female8D") {
            while (horseList >> word) {
                if (word == "Male7C") {
                    cout << "You have Female8D and Male7C.\n";
                    break;
                }
            }
        }
        else if (word == "Male8D") {
            while (horseList >> word) {
                if (word == "Female7D") {
                    cout << "You have Male8D and Female7D.\n";
                    break;
                }
            }
        }
        else if (word == "Female8D") {
            while (horseList >> word) {
                if (word == "Male7D") {
                    cout << "You have Female8D and Male7D.\n";
                    break;
                }
            }
        }
        else if (word == "Male8D") {
            while (horseList >> word) {
                if (word == "Female7E") {
                    cout << "You have Male8D and Female7E.\n";
                    break;
                }
            }
        }
        else if (word == "Female8D") {
            while (horseList >> word) {
                if (word == "Male7E") {
                    cout << "You have Female8D and Male7E.\n";
                }
            }
        }
    }


   horseList.close();

}

【问题讨论】:

  • 我不确定您的目标是什么,您要报告文件中所有不同的单词吗?如果是这种情况,请查看 std::set() 并在阅读时将单词放入一个集合中,然后遍历该集合以报告它们。
  • 所以基本上我想看看我是否有像Male8D和Female7E这样的“马”的某种组合,用户输入文本文件以保存他们拥有的马。如果我有 Male8D 和 Female7E,我希望它输出“你有 Male8D 和 Female7E”。如果我有不同的组合,我希望它输出给用户。该程序的重​​点是首先打印出最好的组合,然后打印出更差的组合,如果你有所需的马组合,甚至只是最好的组合。

标签: c++


【解决方案1】:

您的控制流循环不清楚,无法识别文件中存在的字符串。 假设您想在找到字符串时打印它们,您可以使用 switch-case 语句来相应地执行输出。 或者,您可以为每个字符串分配一个整数并输出结果。 例如,

if (word == "Male8d")
    count+ = 1
if (word == "Female8d")
    count+ = 2

然后根据您的总和打印输出。

【讨论】:

  • 所以程序是这样工作的: 1.用户输入他们拥有的马 2.他们可以检查他们的列表中有哪些马 3.他们使用这个功能来查看他们是否有组合,它应该从最想要的组合到最不想要的组合进行排序。
【解决方案2】:

您提供的程序只能打印前两个匹配项。以下一个可能适用于所有可能的匹配项。

#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <unordered_set>

using namespace std;

int main() {
    string word;
    set<string> matchs;
    // List of words to match
    const unordered_set<string> patterns = {"Female8D","Male7E","Male8D","Female7B"};

    ifstream horseList;
    horseList.open("horseList.txt");
    if (!horseList.is_open()) {
        cout << "Unable to open file...";
        exit(1);
    } else {
        while(horseList>>word) {
            if(patterns.find(word)!=patterns.end()) {
                // record word if it matches
                matchs.emplace(word);
            }
        }
        cout << "You have ";
        for(auto ptr=matchs.begin(); ptr!=matchs.end(); ptr = next(ptr)) {
            // print matched words
            const string & w = *ptr;
            if(ptr==prev(matchs.end())) {
                cout << "and " << w << '.' << endl;
            } else {
                cout << w << ' ';
            }
        }
    }

    return 0;
}

【讨论】:

    【解决方案3】:

    第一

    ifstream horseList;
        horseList.open("horseList.txt");
        if (!horseList.is_open())
        {
            cout << "Unable to open file...";
            exit(1);
        }
    

    好吧,我从未见过“退出”,而不是 exit(),你可以将fourCoats() 设为bool 并且:

    ifstream horseList;
        horseList.open("horseList.txt");
        if (!horseList.is_open())
        {
            cout << "Unable to open file...";
            return false;
        }
    

    现在关于检查这个词,我至少知道两种方法。 第一:

    if (word.find("The word you are looking for") != std::string::npos){
        /*Some code*/
    }
    

    第二: 要重载“==”运算符,如何执行此操作: 在您包含的库之后,您正在使用的命名空间和 您编写的结构/类编写此代码:

    bool operator==(const std::string &str0, const std::string &str1){
        int length;
        if (str0.length() < str1.length())
            length = str1.length();
        else 
            length = str0.length();
        for (int i = 0; i < length; i++){
            if (str0[i] != str1[i])
                return false;
        }
        return true;
    }
    

    第二种方式实际上是做什么的,你说的是“==”运算符,如果两边(左右)都有字符串,那么 for(the_length_of_the_bigger_string){将一个字母的每个字母与上面的字母进行比较另一个位置相同,如果至少有一次字母不相等,则return false; 否则return true;}

    return 结束函数并为该函数设置一个值 int SomeFunc() {/* code */ return 3;},将在我们调用它时将SomeFunc 设置为3 基本上在这个例子中每次都会将它设置为 3 但你知道我的意思 return value_or_variable; 就像结束函数和function = value_or_variable

    如果有人知道改善过载的方法,请写在cmets上,我也需要它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      相关资源
      最近更新 更多