Return 使用关键字的文本文件中的数字

Return a number from text file using a keyword

我有两个文本文件。这两个文件的内容如下所示:

文件1内容: 苹果 5 芒果 10 橙色 15

文件2内容: 苹果 10 芒果 15 橙色 20

我正在尝试制作一个程序,它接受一个关键字(这里是水果的名称)并随机选择一个文件和 returns 与该关键字对应的数值。下面是我的代码。但是,当我 运行 这个程序时,它只显示第一个值而不显示相应的值。我做错了什么?

    double Fruit::Price(string & sym)
    {
        ifstream inResultFile;
        string file_selected;
        int choice;
        string line;

        /*choice = (rand()%2);

        switch (choice)
        {
        case 0:
            file_selected = "file 1.txt";
            break;

        case 1:
            file_selected = "file 2.txt";
            break;
        }*/

        inResultFile.open("file 1.txt", ios::in);
        if (inResultFile.is_open())
        {
            double value=-1;
string name;

            while (inResultFile >> name >> value)
            {
cout<<name<<value;
if(name==sym)
                return value;
            }

        }
        else
            cout << "Sorry, the file could not be openend." << endl;
return -1;
    }

    int main()
    {
        Fruit Obj;
        string symbol;
        double f_Price;

        cout << "Enter a keyword to get the fruit price" << endl << endl;
        cin >> symbol;

        f_Price = Obj.Price(symbol);
        cout << "The selected price of the input symbol is " << f_Price << endl;
        return 0;
    }

1) 您在以下行中销毁了 sym(请求的水果)的值:

    while (inResultFile >> sym >> value)
    {
        return value;
    }

注意:必须依次读取文件,直到达到要求的值,然后才能return。

2) 你永远不会检查从文件中获取的值是否是请求的水果,只是 return 第一次尝试!(也必须在上面的行中发生!)

要获得正确的值,您必须像下面这样比较水果:-

  string fruit = null;
  while(inResultFile >> fruit >> value)
  {
     if( fruit == sym)
         return value;
  }

在你的方法结束时使用下面的行

  else
    cout << "Sorry, the file could not be openend." << endl;
  return 0;//no fruit found 

在 main 中检查 return 值是否为 0,这意味着您选择的水果在文件中不可用。

我刚刚使用了您的以下代码,它是我的工作文件。只需检查您的 txt 输入文件。一定是数据错误

class Fruit
{
   public:
     double Price(string & sym);
};

double Fruit::Price(string & sym)
{
    ifstream inResultFile;
    string file_selected;
    int choice;
    string line;

    /*choice = (rand()%2);

    switch (choice)
    {
    case 0:
        file_selected = "file 1.txt";
        break;

    case 1:
        file_selected = "file 2.txt";
        break;
    }*/

    inResultFile.open("file1.txt", ios::in);
    if (inResultFile.is_open())
    {
        double value=-1;
        string name;
        while (inResultFile >> name >> value)
        {
            cout<<name<<value<<endl;
             if(name==sym)
              return value;
        }

    }
    else
        cout << "Sorry, the file could not be openend." << endl;
        return -1;
}

int main()
{
    Fruit Obj;
    string symbol;
    double f_Price;

    cout << "Enter a keyword to get the fruit price" << endl << endl;
    cin >> symbol;

    f_Price = Obj.Price(symbol);
    cout << "The selected price of the input symbol is " << f_Price << endl;
    return 0;
}

我的输出

Mango
Apple5
Mango10
The selected price of the input symbol is 10