+ 2

How should I fix my C++ Script?

I decided after I finished my C++ Course, I would practice my skills in the code playground. I made a script where you must guess the number four successfully. However, my code playground seems to have a problem with this script. What is my error? Full Script: #include <iostream> using namespace std; int main() { int preciseNumber = 4; cout << "From 1 to 10, what number do I think of?" << endl; int myNumber; cin >> myNumber; if (myNumber == preciseNumber) && (myNumber < 10) && (myNumber > 0) { cout << "Good job!" << endl; } else if (myNumber != preciseNumber) && (myNumber < 10) && (myNumber > 0) { cout << "Sorry, that's not right." << endl; } else if (myNumber > 10) || (myNumber < 0) { cout << "Error, numbers over ten aren't allowed" << endl; } else { return 0; }; }; All known errors: ..\Playground\: In function 'int main()': ..\Playground\:14:39: error: expected identifier before '(' token if (myNumber == preciseNumber) && (myNumber < 10) && (myNumber > 0) { ^ ..\Playground\:14:39: error: expected ';' before '(' token ..\Playground\:18:46: error: expected identifier before '(' token } else if (myNumber != preciseNumber) && (myNumber < 10) && (myNumber > 0) { ^ ..\Playground\:18:46: error: expected ';' before '(' token ..\Playground\:22:31: error: expected primary-expression before '||' token } else if (myNumber > 10) || (myNumber < 0) { ^ ..\Playground\:22:49: error: expected ';' before '{' token } else if (myNumber > 10) || (myNumber < 0) { ^ Please help if you can, it would be great :)

10th May 2017, 8:36 PM
Codemosphere
Codemosphere - avatar
2 Answers
+ 10
Hi, your mistake was in the if statement where you must put all the conditons in one ( ) like this: if ((myNumber == preciseNumber) && (myNumber < 10) && (myNumber > 0)) This is the corrected code, please check: #include <iostream> using namespace std; int main() { int preciseNumber = 4; cout << "From 1 to 10, what number do I think of?" << endl; int myNumber; cin >> myNumber; if ((myNumber == preciseNumber) && (myNumber < 10) && (myNumber > 0)) { cout << "Good job!" << endl; } else if ((myNumber != preciseNumber) && (myNumber < 10) && (myNumber > 0)) { cout << "Sorry, that's not right." << endl; } else if ((myNumber > 10) || (myNumber < 0)) { cout << "Error, numbers over ten aren't allowed" << endl; } else { return 0; } }
10th May 2017, 8:46 PM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 4
@Mr. Atmospheric, When combining a number of conditions in one if condition, you MUST put all of them inside brackets. if( (condition a) && (condition b) || (condition c) ) { //Statements; } For example: In a case where you intend to check whether a person is a youth and not a juvenile, you have two conditions. Condition 1: age <= 36 Condition 2: age >=18 In this case, your if will look like this; if ( (age >=18) && (age <=36) ) { cout<<"You are a youth"<<endl; } @Mohammad Dakdouk got your error right.
10th May 2017, 10:59 PM
STAPS Guru
STAPS Guru - avatar