C++ Homework assignment Movie Recommendation

So far i have it right but I must wait to produce the output until after the loop. Inside the loop, I should only be updating the variables highpercent and movieId2.
After the loop is done, I will use these variables in my output statement.
My problem is i cant figure out how to update the highpercent and movieid2 so i can output it in cout outside the loop.
I have already looked at the other thread but its been close without any further help.

Thanks in advance for help
=====================================================================
.....
.....
.....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int userchoice,  movieid1, movieid2;  
double percent, highpercent=0;
ifstream infile;
infile.open("movies.txt");								

if(!infile)		// file error warning
	cout << " Error locating file.\n";

	else
	{
		cout << " Searching for Receommended Movies.\n";	
		cout << "\n\n";

		while (infile >> movieid1 >> movieid2 >> percent)	
		{
		if ((userchoice == movieid1) && (percent >= highpercent))

				cin >> (infile, percent);
		
				
		}
		infile.close();// file close
			
	cout << " With your choice " << movieid1 << "\t" << setprecision(2) << 
percent << "% also liked movie number " << movieid2 << endl << endl;
												
	}

	system("pause");
	return 0;
	
}
Last edited on
Please, use
[code][/code]
tags.

Other than that, what exactly is supposed to be highpercent?
Also, what is this line cin >> (infile, percent); supposed to do?
Why do you always compare only with movieid1, and not movieid2?

I am sorry, but I personally need more clarity before I can respond.

Regards
txt file has three columns. e.g.
[
movieid1 movieid2 percent
20 20 0
20 6 26.4772
20 18 0.672397
20 12 19.0428
20 9 0.475752
20 3 2.22335
20 17 11.2373
20 16 8.83948
]
cin >> (infile,percent) was suppose to be deleted, imagine it deleted.

movieid1 is the user's input and movieid2 is the movie being recommended by the program based on the percentage.
What i have is highpercent set to 0 and loop reads the file and suppose to update the highpercent along with recommended movie number adjacent to it [movieid2] to the highest percent [in this case 26.47]. and When that is done and called by cout outside the loop, it should say
[ With your choice 20, 26.47% people also liked movie number 6].

so far for every input i get this [ With your choice 14, 0% people also liked movie number 14]
no matter what input i use.

// So the problems i am having are. 1- trying to get the loop to upgrade the percentage 2- output the user movie along with recommended movie with its high percentage.

greatly appreciate your help
The algorithm for finding the maximum for collection of non-negative numbers can be formulated like this:
Max = -1
For each element X in collection
  If X > Max
    Max = X

If you have additional data that is associated with each quantity, and you want to determine not just the maximum value, but record the data that is associated with the maximum value, you can do this:
value of Max = -1
data of Max = whatever
For each element X in collection
  If value of X > value of Max
    value of Max = value of X
    data of Max = data of X

Regards
1
2
3
4
5
6
7
8
9
10
11
12
13
while (infile >> movieid1 >> movieid2 >> percent)	// recommendation loop
{
     if ((userchoice == movieid1) && (percent >= highpercent))

		highpercent=percent;
//cout << " With your choice " << movieid1 << "\t" << setprecision(2) << percent << "% also 
liked movie number " << movieid2 << endl << endl;
		
}
infile.close();// file close

cout << " With your choice " << movieid1 << "\t" << setprecision(2) << highpercent << "% also 
liked movie number " << movieid2 << endl << endl; 


percentage was updated but movieid1 and movieid2 still showing 14. I dont know whats going on.

thanks for the help
Last edited on
Look at may second pseudocode.
- "value of X" is percent
- "value of Max" is highpercent
- "data of X" is movieid2
- "data of Max" is ....?

This is the question you have to answer and then use my second pseudocode.

Regards
I got your code to work somehow but still having problems with movieid2. I dont understand what is data of Max. Max is highpercent and highpercent is set to 0.

if ((userchoice == movieid1) && (percent >= highpercent))

highpercent=percent;
movieid1=userchoice;
moviedid2=???

i am so confuse right now...

thanks
Well, you don't have "data of Max". You need some variable like movieid2_corresponding_to_highpercent. In it, you will store the value of movieid2 that was extracted with the percent value that ranked as highpercent. That is, if you declare such additional variable - movieid2_corresponding_to_highpercent (not with this name exactly), then it will correspond to "data of Max", you will have to update it as per the above pseudocode, and it is the value that you will print in the final summary along with highpercent (instead of movieid2, which simply corresponds to the last extracted movieid2).

Regards
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int movieid1, movieid2, bestmovie;
double percent, highpercent=0;

while (infile >> movieid1 >> movieid2 >> percent)	// recommendation loop
{
if ((userchoice == movieid1) && (percent >= highpercent))

highpercent=percent;
if (bestmovie == highpercent)

movieid2==bestmovie;
movieid1=userchoice;
}
infile.close()


for input 20...I get [with your choice 20, 26.47% people also liked movie number 14]
so i made some progress but i am still stuck on movieid2.

how do i correct it? i followed your second pseudocode and still nothing.

please help
The movieid2 and movieid1 values are overwritten at each cycle, so why do write to them at line 11,12:
1
2
movieid2==bestmovie;
movieid1=userchoice;
The == is a test, so I guess a typo.

You can not compare movie ids with rating percentages. It's like comparing apples to oranges, so this line makes no sense:
if (bestmovie == highpercent)

Constructions like:
1
2
3
if (condition)
  statement1;
  statement2;
make the action of the first statement dependent on the result from the test, but the second statement is not affected by the condition. In fact the second statement is not part of the if in any way. To make both statements part of the if, you need to use:
1
2
3
4
5
if (condition)
{
  statement1;
  statement2;
}

Here is what you ought to have done:
1
2
3
4
5
6
7
8
while (infile >> movieid1 >> movieid2 >> percent)
{
  if ((userchoice == movieid1) && (percent > highpercent))
  {
    highpercent = percent; // value of Max = value of X
    bestmovie = movieid2;  // data of Max  = data of X
  }
}

If you look at the pseudocode, and you make the following association you will see that it matches:
- "value of X" is percent
- "value of Max" is highpercent
- "data of X" is movieid2
- "data of Max" is bestmovie

Also, in pseudocode, conventionally indentation designates the block structure, but I will make it more explicit:
value of Max = -1
data of Max = whatever
For each element X in collection
  If value of X > value of Max
    value of Max = value of X
    data of Max = data of X
  End If
End For

Regards
Thank you so much for your time and help.

I finally figured the problem. First like you said i was missing the if {} brackets. Secondly i had to place movieid1=userchoice outside of the if statement but inside the while loop. And it worked perfectly.

again thanks for much for help and patience. programming is really not my thing.

:)
Topic archived. No new replies allowed.