+ 1

# Logical operator (CPP)

Hello, I created a code by using logical operators and I wanted to create a code that would tell if you sleep enough or not depends on your age and your amount of sleep every 24 hours. The thing is that when I try it, it gives the wrong answers for example the output is: // What is your age 17 How long do you sleep everynight? 10 You don't sleep enough! // And I don't understand because I specified that: // if (age > 13 && age < 18 && sleep > 7) { cout << "You sleep enough!" << endl; // So please can someone help me please? #include <iostream> using namespace std; int main() { int age; int sleep; cout << "What is your age \n"; cin >> age; cout << "How long do you sleep everynight? \n"; cin >> sleep; if (age < 2 && sleep > 13) { cout << "You sleep enough!" << endl; } if (age < 2 && sleep < 14) { cout << "You don't sleep enough!" << endl; } if (age = 2 && sleep > 10) { cout << "You sleep enough!" << endl; } if (age = 2 && sleep < 11) { cout << "You don't sleep enough!" << endl; } if (age > 5 && age < 14 && sleep > 8) {

21st May 2018, 4:30 PM
Sandra.
4 Respuestas
+ 22
yaa,... just the equality comparison is wrong.. as Fatal1 Err0r suggested. it should be == instead of = == checks if the operands r equal = assign value https://code.sololearn.com/czxsJJiFoiP1/?ref=app
21st May 2018, 6:07 PM
🌛DT🌜
+ 5
You put "age = 2" instead of "age == 2". When you do that, you're actually assigning the value of 2 to the variable age, rather than comparing its value to your condition. That's what I notice at first glance. Also, you didn't provide your whole code, so I can't attest to the accuracy of the rest of it. If you want to post it up in code playground and post the link here, I can check it out for you. https://www.sololearn.com/Codes/ ^Code Playground.
21st May 2018, 4:33 PM
Fata1 Err0r
+ 5
#include <iostream> using namespace std; int main() { // Use constants so you don't have // to type "You sleep enough" over // and over again. const string ENOUGH {"You sleep enough!\n"}; const string NOT_ENOUGH {"You don't sleep enough!\n"}; // Initialize these variables so // if you want to implement input // validation later on you have a // base value to compare against. int age {0}; int sleep {0}; cout << "What is your age: "; cin >> age; cout << age << endl; cout << "How long do you sleep everynight: "; cin >> sleep; cout << sleep << endl; if (age < 2) { if(sleep > 13) cout << ENOUGH; else cout << NOT_ENOUGH; } else if (age == 2) { if(sleep > 10) cout << ENOUGH; else cout << NOT_ENOUGH; } else if (age > 5 && age < 14) { if(sleep > 8) cout << ENOUGH; else cout << NOT_ENOUGH; } else if (age > 13 && age < 18) { if(sleep > 7) cout << ENOUGH; else cout << NOT_ENOUGH; } else if (age > 17 && age < 26) { if(sleep > 6) cout << ENOUGH; else cout << NOT_ENOUGH; } else if (age > 25 && age < 65) { if(sleep > 6 ) cout << ENOUGH; else cout << NOT_ENOUGH; } else if (age > 64) { // A hundred million hours of // sleep? really? :-) if(sleep < 100000000) cout << ENOUGH; } return 0; }
21st May 2018, 6:19 PM
Ipang
0
Thank you for the answer here is the whole code: https://code.sololearn.com/cLs0U86yVRaZ/?ref=app
21st May 2018, 5:07 PM
Sandra.
En tendencia hoy
XP farming