简体   繁体   中英

Return a number from text file using a keyword

I have two text files. The contents of both the files look something like this:

File 1 contents: Apple 5 Mango 10 Orange 15

File 2 contents: Apple 10 Mango 15 Orange 20

I am trying to make a program that takes a keyword (here a name of fruit) and randomly selects one of the files and returns the numeric value corresponding to that keyword. Below is my code. However, when I run this program it displays the first value only and not the corresponding value. What am I doing wrong?

    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) You destroy the value of sym (the requested fruit) in the following lines:

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

Note: You must sequentially read the file until you reach the requested value, and after that you can return it.

2) You never check the value which fetched from file is the requested fruit or not, just return the first try!(also it must happen in the above lines!)

to get the correct value you must do a comparison of the fruit like below:-

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

At the end of your method use the below line

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

In main just check if return value is 0 that means the fruit you have selected is not available in the file.

I have just use your below code which is working file for me. Just check your txt input file. Must be data error

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

And my output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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