“Rock the Space”! (Logical operators) | SoloLearn: Learn to code for FREE!

+2

“Rock the Space”! (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.” https://code.sololearn.com/crpAiAUzk0oW/?ref=app

3/1/2021 3:26:02 AM

Tahiti🍷

26 Answers

New Answer

+8

'if' statement with multiple conditions must be put as one parenthesis expression (eventually with nested parenthesis, and ! (not) operator is missused (a not equals b is not a!b, nor a!(==b), but !(a==b) or simpliest a!=b): if (vision==100) && (height>=62) && (height<=75) { and: if (vision!100) || (height!>=62) || (height!<=75)) { should be: if (vision==100 && height>=62 && height<=75) { and: if (vision!=100 || height<62 || height>75) {

+4

Tahiti🍷 Yes but there is not any syntax like that in any programming language. You can write vision != 100 vision < 62 vision > 75 "Is not greater than or equal to" means value should be less than defined value "Is not less than or equal to" means value should be greater than defined value.

+3

#include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if ((vision==100) && (height>=62) && (height<=75)){ cout << "passed"; } else{ cout << "failed"; } return 0; } Is this okay??

+2

K.Suhanya There is word "between" so there should not be >= in your logic.

+2

Okay I Am AJ ! .I just edit else part in Tahiti🍷's coding. Anyway thanks for your comment. 😊

+2

Tahiti🍷 in C as in almost all languages comparison operators are ==, !=, <, >, <=, >=, but not !< nor !<= nor !> nor !>= ;)

+1

Tahiti🍷 What is vision !100 and what is this syntax !>= and !<= You need to learn about Operators again.

+1

also, you could simplify, by replacing the second 'if' statement by an 'else' related to the first 'if' statement...

+1

Tahiti🍷 Program is simple. Read the sentence again. 1 - vision should be 100 % 2 - height must be between 62 and 75. It means height > 62 && height < 75 So condition should be like this if (vision == 100 && height > 62 && height < 75) cout << "passed"; else cout << "failed";

+1

and change parenthesis ^^

+1

or 62.00001, or 74.99999

+1

that's how problem is stated... we often need to decide less or more arbitrarly bounds. In this case the exercise seems to expect bounds not included ;)

+1

#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; if (a == 100 && b >= 62 && b <= 75) { cout << "passed"; } else { cout << "failed"; } return 0; // Hope this helps }

+1

Calvin Sheen Thomas , Yes, this helps tremendously. Thank you! I think the more times I look over the code, the more it begins to “sink in.”

0

#include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if (vision==100 && height>=62 && height<=75){ cout << "passed"; } if (vision !(==100) || height ! (>=62) || height !(<=75){ cout << "failed"; } return 0; } // Expected ‘;’ before ‘!’ token. (???)

0

I Am AJ ! Vision does not equal 100. Is not greater than or equal to. Is not less than or equal to.

0

I Am AJ ! , Are you saying that “greater than or equal to” (>=) doesn’t exist in C++ ? Also, it says that height (not vision) must be between 62 and 75. That is not the same as height>62 && height<75 , which would mean 63 to 74 range.

0

//Here is the code that worked: #include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if (vision==100 && height>=62 && height<=75){ cout << "passed"; } else{ cout << "failed"; } return 0; } // I only had to replace the second “if” statement with an “else” statement.

0

visph You’re right — After changing the parentheses! I tried several times to mark your answer as correct, but the app closed each time I tried.

0

Tahiti🍷 you might try to retry to mark it as best answer... :(