繁体   English   中英

使用关键字从文本文件返回数字

[英]Return a number from text file using a keyword

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

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

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

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

    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;
    }

注意:您必须顺序读取文件,直到达到要求的值,然后才能将其返回。

2)您永远不会检查从文件中获取的值是否是请求的结果,只需返回第一次尝试即可(也必须在上面的行中发生!)

要获得正确的值,您必须对水果进行如下比较:

  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中,只需检查返回值是否为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;
}

而我的输出

芒果
苹果5
芒果10
输入品种的选择价格为10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM