Please someone help me, i dont understand why it doesn't work. And sorry for my English, i dont speak well, im rus | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Please someone help me, i dont understand why it doesn't work. And sorry for my English, i dont speak well, im rus

#include <iostream> using namespace std; int main() { int age; cout « "vvedite znacheniye" « endl; cin » age ; if (age > 19) { if (age < 0) cout « "oshubka " « endl; if (age < 7) cout « "sad" « endl; if (age < 10) cout « "nachalka" « endl; if (age < 18) cout « "starshaya" « endl; } return 0; }

15th Nov 2019, 6:30 PM
Марина Иванова
Марина Иванова - avatar
17 Answers
+ 6
Во первых после каждого if() идет фигурная скобка { за ней cout... и закрывающая фигурная скобка } Во вторых даже, если ты правильно напишешь код у тебя при введении чисел от 0 до 6 будет выводить на экран и sad, и nachalka, и starshaya, потому что числа 0-6 являются меньше 7, 10, 18 Вот так должен выглядеть твой код, чтобы он работал и делал то, что ты хочешь от него: #include <iostream> using namespace std; int main() { int age; cout << "vvedite znacheniye" << endl; cin >> age ; if (age < 0) { cout << "oshubka " << endl; } if (age < 7) { cout << "sad" << endl; } if (age >= 7 and age < 10) { cout << "nachalka" << endl; } if (age >= 10 and age < 18){ cout << "starshaya" << endl; } return 0; }
15th Nov 2019, 11:39 PM
Tom Orrow
Tom Orrow - avatar
+ 3
Just change the line If(age>19) To If(age<19) After this the statements in if block has chance tobe executed.. Just change this one line The program will give the result. Best luck👍
16th Nov 2019, 7:03 PM
Shubham
Shubham - avatar
+ 2
You should check nested if statements lesson in c or cpp
16th Nov 2019, 12:23 PM
Karan
Karan - avatar
+ 2
The problem has nothing to do with the curly braces. C++ allows you to implement an if-statement without curly braces provided only one statement will be in the if-body-statement. The problem with your code is that you have placed one condition: if (age > 19) { // Do everything in this body } So the other if-statements will only execute if the first condition is true i.e age > 19. The other problem is that your other conditions are not realistic sorry to say. For example "age<10" & " age<7". You will have problems getting the right result. So to handle the above consider doing something like: int main() { int age; cout << "Enter age:"; cin>>age; if (age>19) cout<<"makes output only if age is greater than 19\n"; if (age<19 && age >17) cout << "makes output if and only if age is 18"<<endl; }
16th Nov 2019, 4:37 PM
Jacob Chikwanda
Jacob Chikwanda - avatar
+ 2
P.S. Все верно скобки ты можешь опустить, я и забыл об этом. Но вот еще один момент строка if (age > 19) мешает тебе. Она не пропустит тебя если ты вводишь число меньше или равное 19, а твой код имеет смысл только если вводятся числа меньше 18, соответственно получается бессмыслица. Поменяй на if (age < 19) и не забудь про мой прошлый совет - для каждого действия должно быть более четкое условие, например: НЕ if (age < 10) cout « "nachalka" « endl; А if (age >= 7 and age < 10) cout << "nachalka" << endl; Удачи =) #include <iostream> using namespace std; int main() { int age; cout << "vvedite znacheniye" << endl; cin >> age ; if (age < 19) if (age < 0) cout << "oshubka " << endl; if (age < 7) cout << "sad" << endl; if (age >= 7 and age < 10) cout << "nachalka" << endl; if (age >= 10 and age < 18) cout << "starshaya" << endl; return 0; }
17th Nov 2019, 10:29 PM
Tom Orrow
Tom Orrow - avatar
+ 1
You haven't use curly brackets in front of other if statements. In every if statement you have to use open curly bracket every time after condition and before cout and use close curly brackets at the end
16th Nov 2019, 2:43 PM
Moazzam Tariq
Moazzam Tariq - avatar
+ 1
Hello! Make a programme on Vodka.
17th Nov 2019, 4:03 AM
Kausik Kar
Kausik Kar - avatar
+ 1
Just kidding! Nice!
17th Nov 2019, 4:03 AM
Kausik Kar
Kausik Kar - avatar
0
I have been studying c++ for 2 days and i dont know why it doesn't work "Program finished with exit code 0" What does it means?
15th Nov 2019, 6:33 PM
Марина Иванова
Марина Иванова - avatar
0
Марина Иванова , it seems it is successful finish of the program
17th Nov 2019, 9:06 AM
Alexey Likhachev
Alexey Likhachev - avatar
0
:3
17th Nov 2019, 3:01 PM
Jerson Zocadagui
Jerson Zocadagui - avatar
0
Hii
17th Nov 2019, 5:19 PM
ARAVINDAN E
ARAVINDAN E - avatar
17th Nov 2019, 5:27 PM
Abdul Wahab Chattha
Abdul Wahab Chattha - avatar
0
Виталий Познанский, спасибо! С условием "19", да, я что-то забавно ошиблась)
18th Nov 2019, 7:39 AM
Марина Иванова
Марина Иванова - avatar
- 1
Я вроде читала, что если в операторе if только одно действие, то можно не использовать фигурные скобки Но, наверное, я не так поняла Спасибо!! Thanks for all!!
15th Nov 2019, 11:56 PM
Марина Иванова
Марина Иванова - avatar
- 3
12345678987887
16th Nov 2019, 7:00 AM
吳景恩
吳景恩 - avatar