+ 3
ā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
34 Respostas
+ 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.
+ 4
#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??
+ 3
K.Suhanya
There is word "between" so there should not be >= in your logic.
+ 3
Okay I Am AJ ! .I just edit else part in Tahitiš·'s coding.
Anyway thanks for your comment. š
+ 3
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) {
+ 2
Tahitiš·
in C as in almost all languages comparison operators are ==, !=, <, >, <=, >=, but not !< nor !<= nor !> nor !>= ;)
+ 2
#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;
}
Good Luck
+ 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
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.ā
+ 1
Try this!!!!
#include <iostream>
using namespace std;
int main() {
int vision;
cin >> vision;
int height;
cin >> height;
if(vision == 100 && height>=62 && height<=75)
cout<<"passed"<<endl;
else
cout<<"failed"<<endl;
return 0;
}
+ 1
#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" << endl;
}
else {
cout << "failed" << endl;
}
return 0;
}
+ 1
#include <iostream>
using namespace std;
int main() {
int vision;
cin >> vision;
int height;
cin >> height;
//your code goes here
if(height>=62 && height<75)
cout<<"passed";
else
cout<<"failed";
return 0;
}
+ 1
Try this:
#include <iostream>
using namespace std;
int main()
{
int vision;
cin>>vision;
int height;
cin>>height;
if (height>=62 && height<=75 && vision==100){
cout<<"passed"<<endl;
}
else {
cout<<"failed"<<endl;
}
return 0;
}
+ 1
This is my code and it works somehow lol:
#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" << endl;
} else if (vision != 100 || height >= 62 || height <= 75){
cout << "failed" << endl;
}
return 0;