Implenting invalid number code

Writing a program to get the average of 2 scores on a movie. I'm trying to make it so if you input a number less than 0 or greater than 100 it will display "Invalid rating"

When I run it, the program will end after the first "if" statement no matter what number I input

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
  int main()
{
	//Get the name of the movie (The Incredibles)
	string movie = "";
	cout << "What is the name of the movie?: \n";
	getline(cin, movie);

	//Get the Metascore 0 < X > 100
	int metascore = 0;
	cout << "What is the Metascore rating for " << movie << "?\n";
	cin >> metascore;

	if (metascore < 0 && metascore > 100);
	{
		cout << "Invalid Metascore rating, program will end.\n";
		return 0;
	}
	
	//Get the Tomatometer 0 < X > 100
	int tomatometer = 0;
	cout << "What is the Tomatometer rating for " << movie << "?\n";
	cin >> tomatometer;

	if (tomatometer < 0 && tomatometer > 100);
	{
		cout << "Invalid Tomatometer rating, program will end.\n";
		return 0;
	}
if (metascore < 0 && metascore > 100);

This is checking to see if the score is both below zero and above 100. It can't be both at the same time, so this will always be false. Try the logical OR operator || instead of the logical AND &&. (same on the tomatometer if code)

if (metascore < 0 && metascore > 100);
There are a few problems with this.
1) It should be || not &&. How can it be less than 0 and greater than 100 at the same time?
2) The semi-colon at the end should be removed.

Also on line 24 if (tomatometer < 0 && tomatometer > 100); you should remove the semi-colon at the end.
Last edited on
it works now thanks, but could you please explain why there is not suppose to be a semicolon after line 13 & 24
if (metascore < 0 && metascore > 100);
The semicolon ends your if statement right there. It's basically saying

1
2
if (something)
; // do nothing 

Topic archived. No new replies allowed.