I am in a C++ class in college and we have to write a movie recommendation program. We have to open a text file and read data from it. Then sort the data and output it as a movie recommendation. The text file is titled movies.txt. The data in the text file looks like this:
24 8 2.78628
The first number is "movie1", the second number is "movie2" and the last number is "percent". I have to select the highest percent and output percent, movie1 and movie2. There are about 40 lines of data in the same format as above in the movies.txt file.
Here is my code snippet for what I am working with:
if ((userchoice == movie1) && (percent >= bestpercent))
inFile.close();
cout<<percent<<bestpercent<<endl<<bestmovie<<endl<<movie1<<endl<<movie2<<endl;
//cout <<setprecision (2)<< bestpercent << " % of people who liked movie # " << movie1;
//cout<< " also liked movie # " << movie2 << endl;
system ("pause");
return 0;
}
Here is what I do not know:
How do I get the data from the text file to store in a variable which I can output?
How to I sort my data so that the highest "percent" field and correlating movie numbers are selected and output. I am attempting to use my loops to select the data and validate but my output is always the same and clearly does not work. Any guidance would be much appreciated. Thanks!
I solved the program today. I will post the code to show the solution after next wednesday. I do not want to post it earlier out of respect and professional courtesy to the professor as it is not due until then. Essentially my major mistake was closing the .txt file inside the "while(inFile>>movie1>>movie2>>percent)" loop. This loop reads the file from beginning to end and assigns the values found in columns 1-3 to movie1, movie2, and movie3. Thank you all for your help and ideas on this project.