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