0

Logical Operators

Logical operators Along with other physical health requirements, astronaut candidates must have perfect vision (100%) and their height must be between 62 and 75 inches. Write a program that accordingly takes the vision percentage and height (in inches) as an input and prints "passed" if given conditions are satisfied and "failed" if otherwise. Sample Input 100 80 Sample Output failed Solution: #include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if (vision < 100 && height > 75 || height > 62){ cout << "passed" << endl; } else { cout << "failed" << endl; } return 0; } Please help me out here to point an error. It is testing correct 4/5 cases. Thanks in advance!

20th Jun 2021, 12:24 AM
Nick Brown
Nick Brown - avatar
3 odpowiedzi
+ 2
if (vision == 100 && 62 <= height && heigth <= 75)
20th Jun 2021, 12:33 AM
visph
visph - avatar
+ 5
Based on the exercise, the vision must be exactly 100 to pass. But the solution is showing that the astronauts will pass if the vision is less than 100.
20th Jun 2021, 5:17 AM
silentlearner
silentlearner - avatar
+ 3
your test is equivalent to: (vision < 100 && height > 75) || height > 62 when you aren't sure of operator precedences, it's better to set explicit parenthesis ^^ however, you could refer to this link to be aware of operators precedences in C++: https://en.cppreference.com/w/cpp/language/operator_precedence
20th Jun 2021, 12:39 AM
visph
visph - avatar