0

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
    So when you put a break point in on your debugger, what did it tell you the value of 'value' was? – UKMonkey Nov 30 '17 at 16:51
  • When I enter mango, the value I get is 5 i.e the value of the apple. The same happens if I try another keyword too. – Resting Platypus Nov 30 '17 at 16:55
  • 1
    That's not a debugger, that's you running the program and trying to use it. Go read this. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – UKMonkey Nov 30 '17 at 17:04
  • 1
    Read your code out loud to a rubber duck: "while you successfully read a string and a value, return that value immediately". Doesn't it sound a little odd? Shouldn't you compare something to the "symbol"? – molbdnilo Nov 30 '17 at 17:05
  • @molbdnilo. I made the necessary changes. while (inResultFile >> name >> value) { if(name == sym) return value; }. However, the output comes out to be -nan(ind). I even added cout< – Resting Platypus Nov 30 '17 at 17:55

2 Answers2

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!)

Bonje Fir
  • 787
  • 8
  • 25
  • I made changes as noted by you. while (inResultFile >> name >> value) { if(name == sym) return value; }. However, the output comes out to be -nan(ind). – Resting Platypus Nov 30 '17 at 17:23
  • @RestingPlatypus Probably it reads a string as `value`. Since it has not double value, the `value` becomes nan. Use `cout` to debug what program reads. – Bonje Fir Nov 30 '17 at 17:28
  • @RestingPlatypus If it return `-1`, check the file contents. – Bonje Fir Nov 30 '17 at 17:44
  • @RestingPlatypus Another tip: you can comment the line which puts randomness to your code... Just work with one file. Run the program four times with all possible three values and a out of range fruit. Check and debug the it... – Bonje Fir Nov 30 '17 at 17:48
  • @RestingPlatypus 1) Where do you add `cout`? immediately after while or at end of it? 2) Double-check your input file, the nan value may comes from `Mango a 10` situation. i.e it reads string or char instead of double value and put it to `value`. – Bonje Fir Nov 30 '17 at 17:58
  • Removed the randomness. First case: Input is Apple. The price is displayed correctly i.e 5. Second case:- I get the output -1(I added return -1 at the end). Third Case: I entered something not in file. I still get -1. – Resting Platypus Nov 30 '17 at 18:03
  • @RestingPlatypus So add a counter to count how many times the while loop runs. `1` or `3`! – Bonje Fir Nov 30 '17 at 18:04
  • Okay, so the loop runs just once. – Resting Platypus Nov 30 '17 at 18:14
  • @RestingPlatypus Double-check the file. 1) change its name. the program must not be able to find it. 2) Restore its original name and check its content. – Bonje Fir Nov 30 '17 at 18:17
  • Thank you so much for your help. It turns out that their was an anomaly in the contents. I was missing one field. Made the necessary changes in the file. The code works now. Thank you again. – Resting Platypus Nov 30 '17 at 18:26
0

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

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17